Advanced Systems Programming:: Marcus Völp
Advanced Systems Programming:: Marcus Völp
Marcus Völp
Schedule for today
• C++ Types
– fundamental types
– class, struct, bitfields
– union
– arrays
– typedef and new types
• Pointers + References
– Introduction
• C++ Types
– fundamental types
– class, struct, bitfields
– union
– arrays
– typedef and new types
• svn
Pointers + References
co http://svn.inf.tu-dresden.de/repos/advsysprog/day2/examples_lecture
– Introduction
Types
• define the operations that are permitted on an object
• define the size of the object representation
y = x;
y = x + 42;
y = f(x);
• compare
• fundamental types:
• signed char ⊆ short int ⊆ int ⊆ long int ⊆ long long int
• unsigned integers:
– nonnegative signed range ⊆ unsigned range
– nonnegative values represented by the same bit pattern as
signed
– arithmetic modulo 2^n
class Complex {
private:
int _real; fundamental type: int
int _imag;
public:
void add(int n); compound type: void (Complex::)(int)
};
Access restrictions:
public – everyone
private – only class members, (friends - later)
protected - only class and derived classes
class Complex {
private:
int _real; fundamental type: int
int _imag;
public:
void add(int n); compound type: void (Complex::)(int)
};
Access restrictions:
public – everyone
private – only class members, (friends - later)
protected - only class and derived classes
S a;
T b;
b = a;
• GCC seems to always chose the same layout for Pack1 and Pack2!
Bit Fields:
0 31
ptab_base avail p
p avail ptab_base
struct page_table_entry {
unsigned present : 1;
unsigned avail : 11; /* 8-bit char + 3-bit padding */
unsigned base : 20;
};
alternatively:
unsigned avail : 8;
: 3; /* padding */
char c = 'M';
char* p = &c; /* pointer to c */
char& r = c; /* reference to c */
p:
c: 'M'
r:
class C {...};
C c; object on stack
C* p = new C(); pointer to object on heap
C* p = &c; pointer to existing object
1) single-linked list
3) double-linked list
sum
- iterate list and sum up elements individually vs.
- maintain sum at list head
• Friends
• Inheritance + Casts
– static cast, reinterpret cast
– dynamic cast
• Pointer to arrays
– pointer arithmetic
– ring buffer (Hacking: optional)
class ListElement {
private:
ListElement * _next;
};
...
void List::insert_head (ListElement * elem) {
elem->_next = _head;
_head = elem;
}
class ListElement {
friend void List::insert_head (ListElement * elem);
friend class List; /* all members of List are friends of ListElement */
…
};
class Insect {
public: Insect
static unsigned int const number_of_legs = 6; number of legs = 6
};
Bee b;
unsigned int foo = b.number_of_legs;
Casts
Moscito* m = static_cast<Moscito*>(i);
Insect Flying_thing
number of legs = 6 void fly()
Aerodyn. Thinkpad
• Generic Interface
• Generic Implementation
• Different implementation for each derived class
Aerodyn_notebook * aero_notebook;
Insect Flying_thing
number of legs = 6 void fly()
class Flying_things {
virtual void fly() = 0; => Bee must implement void fly()
} to become non virtual
class Complex {
int _real;
int _imag;
public:
void init();
};
void Complex::init()
{
_real = 42; _imag = 0;
}
class Complex {
public:
Complex(); /* Default Constructor for Complex */
Complex(const Complex& c); /* Copy Constructor */
Complex(int real, int imag); /* Initialising Constructor */
Complex(int real); /* Conversion from int to Complex */
}
October, 2nd 2009 Marcus Völp 27
Construction / Destruction (take 1)
delete middle;
class List_Element {
public:
~List_Element();
};
List_Element::~List_Element()
{
remove();
}
• Arrays
• Operator Overloading
– overloading && for non base types
• void *
• References
– call by value vs. call by reference
– self reference
T a[6];
T a[6];
sizeof(T)
T a[6][2];
T
T
T a[6];
T a[6][2];
T
T
T a[3][4];
T
T
T
T a[6];
p illegal – avoid it
e legal pointer to end of array – do not dereference
*e undefined
m' = m + 1 next element in the array that follows *m
this is NOT m + sizeof(T) but &(*m) + sizeof(T)
m–f number of elements of type T in between m and f (here 3)
Advice [ST – 5.8]: Avoid nontrivial pointer arithmetic !!! DOCUMENT IT !!!
October, 2nd 2009 Marcus Völp 35
Operator Overloading <overloading.cc>
Operators:
+ | - | * | / | % | ^ | & | | | ~ | ! | = | < | > | += | -= | *= | /= | %= | ^= | &= | |= |
<< | >> | >>= | <<= | == | != | <= | >= | && | || | ++ | -- | ->* | , | -> | [] | () |
new | new[] | delete | delete[]
Example:
Complex a, b, c;
c = a + b; c = add(a,b)
c = 7; c = Complex(7); Constructor for
int to complex conversion
Operators:
+ | - | * | / | % | ^ | & | | | ~ | ! | = | < | > | += | -= | *= | /= | %= | ^= | &= | |= |
<< | >> | >>= | <<= | == | != | <= | >= | && | || | ++ | -- | ->* | , | -> | [] | () |
new | new[] | delete | delete[]
Iterators:
List_element le;
le++; le--; le->f_T(); le = le->next(); le = le->prev(); le->item()->f_T();
Allocators:
class T {
public:
unsigned int val;
};
int * pi;
Typical use:
Give a hint to the compiler that the object will not be modified!
Const objects may still need storage if the compiler cannot derive
that they are remain constant!
int const c;
int i = const_cast<int>(c);
Constructors Revisited
class Aerodyn_notebook {
unsigned int const _memory;
unsigned int const _cpu_freq;
class Matrix {
unsigned int _data[200][200];
}
self reference
class Matrix {
Matrix operator + (const Matrix & a)
}
• Underlying Storage
– object lifetime
• Scope
– lock guards
Static storage
• non local, not dynamically created objects
int global;
class X {
static unsigned int count_of_x;
}
Automatic storage
• local objects, auto / register
int foo () {
int local;
return 42;
}
{} defines a scope
int foo () {
/* begin scope of foo */
if () {
/* begin scope of if case */
Dynamic storage
• dynamically created objects
C* c = new C();
new[]
• Problem:
Guarantee that a function is invoked on a long living object before the scope is
exited. E.g., release lock
• Objects with automatic storage location are destroyed when the scope is exited
class Lock {
void acquire();
void release();
}
Lock_Guard::~Lock_Guard() {
_lock.release();
}
October, 2nd 2009 Marcus Völp 50
The End
(for today)
• List:
+ size can be dynamic
+ co-location of list elements and large objects
+ sort without copying the object
- sort requires many queue operations
- allocation and deallocation overhead
- memory overhead for pointers
• often:
– FIFO of small elements
– Number of elements are known a-priori
head tail
1 0 1 2 2 1
2