Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?
This is most confusing question for and beginner of c programming language. They dont know the all segment name and dont know on which segment which data are going to store . So here i am just giving you the brief idea about the structure of any c program in memory.
This structure can be divided in 3 parts .
- Data Segment
- Initialized data segment (initialized to explicit initializers by programmers)
- Uninitialized data segment (initialized to zero data segment – BSS [Block Start with Symbol])
- Code Segment
- Stack and Heap areas
1> Data Segment
The data segment contains teh global and static data that are explicitly intialized by the users containing the intialized values.
The other part of data segment is called BSS (because of the old IBM systems had that segment intialized to zero). It is the part of memory where the OS initializes the memory block to zeros. That is how the uninitialized global data and static get default value as zero. This area is fixed and has static size.
The data area is separated into two areas based on explicit initialization because the variables that are to be initialized can be initialized one-by-one. However, the variables that are not initialized need not be explicitly initialized with 0′s one-by-one. Instead of that, the job of initializing the variable is left to the OS. This bulk initialization can greatly reduce the time required to load the executable file.
Mostly the layout of the data segment is in the control of the underlying OS, still some loaders give partial control to the users. This information may be useful in applications such as embedded systems.
This area can be addressed and accessed using pointers from the code. Auto variables have overhead in initializing the variables each time they are required and code is required to do that initialization. However, the varialbes in the data area does not have such runtime overload because tha initialization is done only once and that too at loading time.
2>Code segment
The program code is the code area where the executable code is available for execution. This area is also of fixed size. This can be accessed only be function pointers and not by other data pointers. Another important information to note here is that the system may consider this area as read only memory area and any attempt to write in this area leads to undefined behavior.
Constant strings may be placed either in code or data area and that depends on the implementation.
3>Stack and heap areas
For execution, the program uses two major parts, the stack and heap. Stack frames are created in stack for functions and heap for dynamic memory allocation. The stack and heap are uninitialized areas. Therefore, whatever happens to be there in the memory becomes the initial (garbage) value for the objects created in that space.
Lets look at a sample program to show which variables get stored where,
int initToZero1; static float initToZero2; FILE * initToZero3; // all are stored in initialized to zero segment(BSS) double intitialized1 = 20.0; // stored in initialized data segment int main() { size_t (*fp)(const char *) = strlen; // fp is an auto variable that is allocated in stack // but it points to code area where code of strlen() is stored char *dynamic = (char *)malloc(100); // dynamic memory allocation, done in heap int stringLength; // this is an auto variable that is allocated in stack static int initToZero4; // stored in BSS static int initialized2 = 10; // stored in initialized data segment strcpy(dynamic,”something”); // function call, uses stack stringLength = fp(dynamic); // again a function call } |
You may like to read this also....
5 Comments to “Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?”
Post comment
Search in this website
our sponsors
latest comments
- sagar on List of all standard version of c language
- Mohit Dhukia on How to access/unblock songs.pk in india?
- shinto peter on How to configure mail from localhost ( wamp ) using PHP?
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language
Very nice! Keep em’ comming.
You write that: “Another important information to note here is that the system may consider this area as read only memory area and any attempt to write in this area leads to undefined behavior.”.
Q: How about read? Would read result in undefind behaviour? (Direct memory read). Can one, with correct privilegies, create a pointer, start at address one, and dump entire RAM content?
here you are talking about code segment …then i have already said this segment can only read by function pointer not by data pointer. for taking dump of RAM you need to use data pointer so in this segment this will cause “undefind behaviour”
JIGAR PATEL recently posted..How to access/unblock songs.pk in india?
Hello Jigar,
Thanks for posting such nice information,
I have one query, how memory will be allocated for variable those have diffrent storage class in structure?.
I mean in which section they will be allocated. connsider structure is not static ar nor not using any qualifier just structure variables has diffrent storage class.
Ex.
Struct student
{
static int class,
int roll no,
char name[20],
};
}
How a structure will be stored in memory in c. For example
struct node
{
int a;
char *p;
}var;
Very nice information buddy,keep it up, Pls explore strlen,how to find it’s definition
size_t (*fp)(const char *) = strlen;