Monday, October 20, 2014

Inline Functions

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided.
A function is declared inline by using the inline function specifier or by defining a member function within a class or structure definition. 

The inline specifier is only a suggestion to the compiler that an inline expansion can be performed; the compiler is free to ignore the suggestion. 

The following code fragment shows an inline function definition. 

inline int add(int i, int j) { return i + j; }
 
The use of the inline specifier does not change the meaning of the function. However, the inline expansion of a function may not preserve the order of evaluation of the actual arguments. Inline expansion also does not change the linkage of a function: the linkage is external by default.

Wednesday, October 1, 2014

Difference between shallow copy and deep copy

When you do a shallow copy, all the fields of the source object is copied to target object as it is. That means, if there is a dynamically created field in the source object, shallow copy will copy the same pointer to target object. So you will have two objects with fields that are pointing to same memory location which is not what you usually want.
In case of deep copy, instead of copying the pointer, the object itself is copied to target. In this case if you modify the target object, it will not affect the source. By default copy constructors and assignment operators do shallow copy. To make it as deep copy, you need to create a custom copy constructor and override assignment operator.

C++ Storage Classes



Storage classes are used to specify the visibility/scope and life time of symbols (functions and variables). That means, storage classes specify where all a variable or function can be accessed and till what time those variables will be available during the execution of program.
Following storage classes are available in C++,

  • Auto:
Auto is the default storage class for local variables. They can be accessed only from within the declaration scope. auto variables are allocated at the beginning of enclosing block and deallocated at the end of enclosing block. 

  • Register:
Register storage class is similar to auto variables. Difference is that register variables might be stored on the processor register instead of RAM, that means the maximum size of register variable should be the size of CPU register ( like 16bit, 32bit or 64bit). This is normally used for frequently accessed variables like counters, to improve performance. But note that, declaring a variable as register does not mean that they will be stored in the register. It depends on the hardware and implementation. 

  • Static:
A static variable will be kept in existence till the end of the program unlike creating and destroying each time they move into and out of the scope. This helps to maintain their value even if control goes out of the scope. When static is used with global variables, they will have internal linkage, which means it cannot be accessed by other source files. When static is used in case of a class member, it will be shared by all the objects of a class instead of creating separate copies for each object. 

  • Extern:
extern is used to tell compiler that the symbol is defined in another translation unit (or in a way, source files) and not in the current one. Which means the symbol is linked externally. Extern symbols have static storage duration, which is accessible throughout the life of program. Since no storage is allocated for extern variable as part of declaration, they cannot be initialized while declaring.



  • Mutable:

mutable storage class can be used only on non static non const data a member of a class. Mutable data member of a class can be modified even if it's part of an object which is declared as const.
 





String Table

A string table is a Windows resource that contains a list of IDs, values, and captions for all the strings of your application. For exa...