Search This Blog

Saturday, August 1, 2009

Programming Facts

3 comments:

  1. An inline functions are expanded at the calling block. They are treated as macro definition by compiler. Usually, inline functions have smaller block of code.

    In fact, you do not need to declare function inline. If a function have small block of code, they are treated as inline and if they have large block of code, even though if they are declared inline, they are treated as regular function.

    According to syntax, inline functions can not have recursion.

    ReplyDelete
  2. When a program is compiled, object gets bind to the functions they call. This is called static binding. But, if the called function is defined virtual, binding happens at run-time. This is called dynamic binding.

    Whenever a program has virtual function declared, a v-table is created for each class containing virtual functions. The v-table of any particular class records addresses of all the virtual function declared in that class. An Object of a class with virtual function/s contains a pointer variable for each virtual function pointing to the starting address of v-table. So, whenever this object calls a virtual function, v-table is used to resolve the address for the respective function. This is how dynamic binding works.

    Usually, destructor functions of a base class is declared as virtual to ensure that destructors are called in correct order. This prevent the memory leak. Constructors of a class can not be declared as virtual because v-table of that class is not available at the time when a constructor is called.

    ReplyDelete
  3. There is a big difference in how memory is allocated for two important datastructures - Union and Struct. All the variables declared in Struct datastructure are alloted their individual memory space; where as the variables declared in Union datastructure shares their memory space. This might be the reason for the name "Union". The memory size for the Union datastructure is equal to the maximum of the memory sizes of the variables declared into that datastructure; where as memory size of Struct datastructure is equal to the sum of memory sizes of all variables declared into that datastructure.

    ReplyDelete