Dear Anish,
Here I am giving Memory Layout of C programme.
Generally C-programme has following memory segements.
- Code Segment : All instructions are stored here.
- Text Segment(read only segment) : all strings, constant are stored here
3 Data Segment : Static and gobal variables are stored here.
4 Stack Segment : Local variables, function parameters, return values …etc
5 Heap Segment : All dynamic memory allocations done here.
Data Segment has 2 subsections, Why?
Pleas find the following description which I have given long back.
static int x, &
static int x =5,
In both statements X is stored in data segment only,
But the difference is internally Data segment have 2 sections
a) Intialised section
b) un-Intialised section
if we use first statement
static int x, then x is stored in un-Initialized section
if we use Second statement
static int x =5,then X is stored in Initialized section
the Reason why again divided the Data segment into 2 sections
it is easy to initialized all un-initialized static variables to
Zeroes (using memset?), If all Un-Initialized data are stored at same
place.
please find the following example
Int a; // Un Initialized Global variables, stored in Un-Initialized
Data segment.
Int q
; // Initialized Global variables, stored in Initialized Data segment.
Void Main()
{
Int x; // Auto Variable ( Stored in Stack Segment)
Int y=10; // Auto Variable ( Stored in Stack Segment)
Const int z=25; // Stored in Stack Segment
Char *ptr=0;
Ptr=malloc/calloc( 100 bytes);// dynamic memory allocation( allocated from Heap
Segment)
Char *str=" I am a good buy "; // This String is stored in Read Only
Segment (we can also say Text Segment)
Static int p=10;// Initialized Static variables, stored in
Initialized Data Segment.
Static int x; // Un Initialized Static variables, stored in
Un-Initialized Data Segment.
Fun( 10,20);
}
Fun(int x,int y) // function parameters x and y variables are stored
in Stack segment.
{
Return 10,// Return values are stored in Stack Segment.
}
Please let me know if u need any info.
Thanks and Regards
Ajay kakumanu