Saturday, December 5, 2009

Variable declaration and definition

What is BSS?
BSS, a part of Data Segment store all variables initialized to 0. static variable(initialized with value other than 0) are not stored in BSS.
Actually BSS is an "Uninitialized RAM" which is initialized to 0 before executing main().



"static" variables are hold in the heap memory and "auto" variables are stored on the stack.

Heap(Data Segment & BSS(Block start by symbol))
Static varibales are neither stored in stack nor in heap. Static varibales are stored in data segment.

Auto variables are stored in stack and dynamic allocated variables are stored in heap.



Memory allocations of structure
I think slack memory is the extra memory at the end of a structure which may go unused. It may also include unused memory within a struct. For example if a struct includes a short and then an int in that order the int needs to be on a 4-byte boundary but the short needs to be on a 2-byte boundary. Thus 2 bytes go to waste. If a stack is allocated from the heap the memory manager may fragment the heap in a manner where is unused memory contiguously after the stack object. This definition is cloudy as I couldnt find a real crisp defiition of "slack memory".



Can static variables be declared in a header file?

You can’t declare a static variable without defining it as well (this is because the storage class modifiers
static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended. 

No comments:

Post a Comment