Jan
7

Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?

Author JIGAR PATEL    Category c, Tips     Tags

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 
}

 

 

JIGAR PATEL

hey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here JigAr Patel

More PostsWebsiteTwitterFacebook

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?”

  • Void March 14, 2012 at 9:05 pm

    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?

    • JIGAR PATEL March 15, 2012 at 4:40 am

      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? My Profile

  • kamlesh khot July 17, 2012 at 9:02 am

    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],
    };

    }

  • prashant January 23, 2013 at 10:56 am

    How a structure will be stored in memory in c. For example
    struct node
    {
    int a;
    char *p;
    }var;

  • vaibhav February 10, 2013 at 9:31 am

    Very nice information buddy,keep it up, Pls explore strlen,how to find it’s definition
    size_t (*fp)(const char *) = strlen;

Post comment

*

CommentLuv badge
Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link