Discussion Review Sheet C ========================================================== *Preprocessor ** #define Textual substitution -- watch out for side effect in arguments MAX (a++, b++) --> (a++>b++) ? a++: b++ Do functions have the same problem with arguments? Single instance substitution #define add(x,y) x + add(0, y) add(x,y) --> x + add(0, y) --> NOT x+ 0 + 0 + ... ** #include Include text of another file. Does it need to be a .h? Use of #ifdef #else #endif 1. Header files (multiply includes) 2. Condition compilation (DEBUG option) * Type and Expressions basic types (int , char, double, etc) --> Bool is not a basic types in C constructed types (struct, union) casts automatic coerced (type)value (float)n (char*)p; operators arithmetic +, -, *, /, % bitwise & | ^ ~ >> << relational (>, <, ==, !=, &&, ||, !) assignment (=, +=, etc) special ++, --, ?:, comma (used in for statements) *, & (unary version) *Control FLow if, while, for, goto, switch, do-while continue, break 1. Switch cases must end with a break (flow through case labels) 2. Continue finds the closest enclosing loop 3. break find the closest enclosing loop or switch. * Storage/pointers sizeof (char) == 1 byte sizeof (int) == ? bytes (on 32 bit machines 4). sizeof (void*) == sizeof (char*) == 4 bytes (on 32 bit machines) globals: int n; int an[10]; int *pi = &n; 100 : xxxx : n 104 : z0 : an[0] 108 : z1 : an[1] ... 144 : 100 : pi Address arithmetic: pointers + int, pointer - int, pointer - pointer (if point to same array) Declarations: char a[] = "hello"; sizeof a == 6 sizeof *a == 1 char *p = "hello"; sizeof p == 4 sizeof *p == 1 char b[10] = "hello"; sizeof b == 10 sizeof *b == 1 arrays of pointers char *months[] = { "jan", "feb", "mar" , ... , "dec"}; allocates 12 * sizeof (char*) char months[][6] = { "jan", "feb", "mar", ... , "dec"} allocates 12 * sizeof char [6] Multidimensional int ai[10][10]; /* allocate 100 int's */ int *pi[10]; for (...) pi[c] = malloc (sizeof (int) * 10) Whats the difference with a) ai[3][4] b) pi[3][4] ai[3] --> An array of 10 ints or (ai + 3) ai[3][4] --> index the element of array pi[3] --> A pointer to an array of 10 ints ai :100 : ai[0][0] integer values ... ai[0][1] 140 : ai[1][0] ... 500 : pi :100 : x ... 140 : y ... x : pi[0][0] x+4 : pi[0][1] ... y : pi[1][0] *Structures/Unions Maintain a set of values as a single type Declaration struct name { ... }; Use struct name myname; Maintain exactly one type of a set og types; union overlay { int i; double d; }; // overlay has either int or double union overay b; b.d = 1.0; // assign double b.i = 1; // assign int -- overwrites double storage printf ("%f", b.d); // Garbage printf ("%d", b.i); // OK. Predeclarations struct nodeA; struct nodeB; struct nodeA { struct nodeB *next; ... }; struct nodeB { struct nodeA *next; ... }; *Scope and linkage Scope: a declaration exists till the end of enclosing block { int a; // a enters scope ... } // no longer in scopt Extent: The lifetime an object (memory) may exist though it is not is an enclosing scope { static int a; } // a's storage is still valid for Since module may be compiled separately extern declarea a function or object to be available for linkage to other modules static specifies internal (module) linkage. C++ ========================================================== 1. class vs. struct constructor/destructors 2. access specifiers for members public, private, protected Permitted members field , methods, class, typedef's 3. overloading function, methods, operators 4. inlining vs. macros (#define) Compiler performs substitution (vs preprocessor) uses copy semantics for arguments. 5. Inheritance public, private, protected method hiding (when does method hide a method in the base class) virtual methods (polymorphic calls). 6. static members static field member static method 7. References vs. pointers initialization use of in operators 8. const object reference method