Monday, December 22, 2008

C Language

Some points about C

  • Memory have Code segment, Data segment,Stack segment and heap area (in the user mode) and kernel stack and u area (in kernel mode).

  • Variables :- A variable is the name given to the memory location where constant resides. The location of variable inside the memory depends on the storage class. There are five storage class in C (Auto, Register,Static,Extern and typedef Storage class)  
*   The variable with  auto storage class  stored in Stack part of the memory.
*   The variable with register storage class stored in registers of CPU (only if registers are available otherwise the register storage class is treated as auto class).       
   *  The variable with static storage class stored in data segment.
   *  The variable with extern storage class stored in data segment.

Data segment have two section : - Initialized section and Uninitialized section. The initialized part of the data segment is itself have three parts :- Read Only(having all constants like string) , Read/Write (having global variable) and static (having static variable). and the uninitialized part is divided into BSS (Block Started by Symbol) {The variables like global and static which have default values as 0 are stored here if not initilized with some other value}and ES (Extra Segment).

  • The life of an object determines whether the object is still in the memory (of process), scope of the object decides whether the variable can be assessed at that point in the program.

  • The stack contains all local variables of the function, function arguments and function framne and has two dedicated register base register and stack pointer.

  • Heap memory is used for runtime allocation of memory. It gets refrence from the stack or from the BSS etc. Holes or free space in the heap can not be accessed by user mode.

  • If the memory is freed, the memory do not get derefrenced and has the refrence of free memory, these refrences are called dangling pointers. Hence as soon as memory is freed, the refrence should also be freed by pointing it to null.

  • If a pointer to one memory location is made to refrence another memory location, then the refrence to that memory location got lost and this result into memory leak.

  • A structure declaration that is not followed by a list of variables reserves no storage, It marely describes a template or shape of structure.