Preprossessor

Preprosessor: A programs that runs before compiler and changes source text of code. Inherited from C to C++.
Preprosessor variable: A variable that managed by preprossessor. The preprocessor replace each preprosessor variable by its value before program is compiled. The name should unique in programs hence usually make is all CAPITAL.
header guard: preprosessor varialbe that used to prevent header file from being included more than once in a single file.

  1. // #include replace the content by the specified header file.
  2. #include<iostream>
  3. #include "ilib.h"
  4. // #ifdef ifndef returns true if preprosessor varialbe is defined/not. It match with #endif
  5. #ifdef ILIB_H
  6. ...
  7. #endif
  8. // #define will define the name as preprosessor varialbe.
  9. #define TEST
  10. // we can use following in header file to prevent multiple inclusion
  11. // the first time header file included, ILIB_H is not define and content will execute
  12. // the following time header guard already defined, content is skipped.
  13. #ifndef ILIB_H
  14. #define ILIB_H
  15. ... CONTENT OF HEADER FILE ...
  16. #endif

assert(expr)

assert is a preprosessor macro (it is a preprosessor variable that acts somewhat like inline function). If expr is false, the assert writes a message and terminal the program. Otherwise it does nothing. It is defined in casssert.h

The behavior of assert depend on if preprocessor variable NDEBUG is defined. If defined, assert does nothing. By default NDEBUG is not defined. The following can be used to turn off assert

  1. CC -D NDEBUG main.c #linux
  2. CC /D NDEBUG main.c #win

Aside from assert, modernC++ programs make very little use of preprocessor macros.

In addition we can use following aids debugging.

  1. // cerr for standard error terminal
  2. cerr << "Error: " << _ _FILE_ _ // filename
  3. << " : in function " << _ _func_ _ //compiler defines it in every function. It is a static array of const char
  4. << " at line " << _ _LINE_ _ << endl // line number
  5. << " Compiled on " << _ _DATE_ _ // date
  6. << " at " << _ _TIME_ _ << endl // time

Struct

Struct in C

  1. struct [structure_name] //struct_name can be omitted.
  2. {
  3. member definition;
  4. member definition;
  5. } [one or more structure variables];
  6. // usage
  7. struct struct_name iStruct;
  8. iStruct.id = 4;
  9. struct struct_name *ptr_iStruct;
  10. ptr_iStruct = &iStruct;
  11. ptr_iStruct->id = 4;
  12. // with typedef
  13. typedef struct Student {
  14. int age;
  15. } S;
  16. // equivalence
  17. struct Student {
  18. int age;
  19. };
  20. typedef struct Student S;

now here S is same as struct Student

Reference

Static

Good references: Link1 Link2

Static elements are allocated storage only once in a program in static storage area, and they have a scope till the program lifetime. Usage:

  1. Static ordinary variable in function
  2. Static ordinary function
  3. Static Class objects
  4. Static Class member variable
  5. Static Class function

1. Static ordinary variables in function

It only initialized once and stored in static area (not in stack). Hence when function ends it still exist and value holds. When function is recalled the static variable holds its previous value and reusable.

  1. void counter(){
  2. static int counter = 0;
  3. cout << counter++;
  4. }
  5. int main(){
  6. for(int i=0; i < 5; i++){
  7. counter();
  8. }
  9. }
  10. // OUTPUT
  11. // 0 1 2 3 4

2. Static ordinary function

In C a static ordinary function have its scope only in the file it declared (default function have scope globally). The advantage is we can reuse the same function name in other files.

  1. static void print_filename(){
  2. cout << "Test" << endl;
  3. }

3. Static Class Objects in function

Similar to static ordinary variables. The static class object is initialized only once (constructor is called only once) and have scope till end of the program (destructor is called at end of program, not the scope of the function)

  1. class iClass{
  2. int i;
  3. public:
  4. iClass(){
  5. i = 0;
  6. cout << "CONSTRUCTOR" << endl;
  7. }
  8. ~iClass(){
  9. cout << "DESTRUCTOR" << endl;
  10. }
  11. void add_one(){
  12. cout << "i updated to " << ++i << endl;
  13. }
  14. };
  15. void iTest(){
  16. static iClass iObj;
  17. iObj.add_one();
  18. }
  19. int main(){
  20. for(int i = 0; i < 5; i++){
  21. iTest();
  22. }
  23. cout << "END MAIN" << endl;
  24. }
  25. // OUTPUT
  26. // CONSTRUCTOR
  27. // i updadted to 1
  28. // ...
  29. // i updadted to 5
  30. // DESTRUCTOR

4. Static Class member variables

Static class member variable is shared by all class objects. It have only one single piece of storage (for normal member variables, each object got a copy of it).

Static class member variable is not initialized by constructor, because it is not dependent on object initialization. Hence it must initialized explicitly, always outside the class. If not, Linker will will give error.

5. Static class member function

Static class method work for the class as a whole rather than for a particular object of a class. Typically is called by classname::function() .
Static class method cannot access normal class member and member function, but only static data members and functions. The reason is that it doesn’t have this keyword.

// pt 4 and 5
class iClass{
public:
    static int counter;

    iClass(){
        counter++;
        cout << counter << " obj created!" << endl;
    }

    static void all_obj(){
        cout << "Total " << counter << " objects" << endl;
    }
};

int iClass::counter = 0;

int main(){
    iClass obj1;
    iClass obj2;
    iClass obj3;
    iClass::all_obj();
    return 0;
}

// OUTPUT
// 1/2/3 obj created!
// Total 3 objects

Inline function

  • Can be used to speed up code - When function is called, the program, too much overhead
    1. store the instruction following the function to memory
    2. load the function code into memory, Copy variables to memory
    3. jump to the memory where function is located, and execute, store the returned value
    4. jump back to execute next code
  • Just like Macro, it is doing code expansion (copy paste), so function call is reduced and increased the readability
    • Compared to macro functions, inline functions do security checks or automatic type conversions (as with normal functions) when code is expanded, while macro definitions do not.
  • The compiler generally does not inline inline functions that include complex operations such as loops, recursion, and switch
  • All class member functions are implicitly inline function

Link for more details