Memory layout of a C program


Below is the typical memory layout of a c program.

                                             Higher- Numbered Address
  Command line arguments
   and environmental variables
Stack
|
V

/\
|
Heap
Uninitialized data segment
Initialized data segment
ROdata
Text segment
                                             Lower-Numbered Address

Let us use the below example(c program) to understand the memory layout of a C program.

  /* memory_layout.c */
  #include <stdio.h>
  #include <stdlib.h>

  /* initialized and uninitialized globals */
  int init_global = 10, uninit_global;

  /* static global variable */
  static int static_variable = 100;

  /* string constant */
  const char str[] = "see-programming";

  /* numeric constant */
  const int num = 10;

  void fun2() {
        int *ptr, num1, num2;
        /* dynamic memory allocation */
        ptr = (int *) malloc(sizeof(int));
        *ptr = 100;
        free(ptr);
        return;
  }

  void fun1() {
        fun2();
        return;
  }

  int main() {
        fun1();
        return 0;
  }

Let us use GDB(GNU debugger) to poke around inside the C program which we are executing.  Basically, GDB works on executable file.  During compilation of the C program, we need to use -g option to produce debugging information for use by GDB.

  jp@jp-VirtualBox:~/$ gcc -g mem_layout.c -o memlayout
  jp@jp-VirtualBox:~/$ ls
  memlayout     mem_layout.c
  jp@jp-VirtualBox:~/$ gdb ./memlayout
  GNU gdb (GDB) 7.2-ubuntu
  (gdb) break fun2
  Breakpoint 1 at 0x80484a8: file mem_layout.c, line 36.
  (gdb) run
  Starting program: /home/jp/cpgms/lab_pgms/temp/memlayout "INDIA"

We have created an executable named memlayout and loaded the same in GDB. We have set a break point(break fun2 - pauses execution at the given location) at function fun2().

Text Segment:
It has the machine codes and the CPU executes these machine codes.  Inorder to prevent machine codes from being over written by stack or heap overflow, it is kept read only.  It is located below the stack and heap segments.  Symbols correspond to various functions in the given C program will also be available in text segment.  And it can be accessed using function pointers.

Let us find the address of the various functions in our C program using GDB.
  (gdb) print main
  $6 = {int ()} 0x804842c <main>
  (gdb) print fun1
  $7 = {void ()} 0x804841f <fun1>
  (gdb) print fun2
  $8 = {void ()} 0x80483f4 <fun2>

RO Data Segment:
It is also called as Read Only Segment.  If const qualifier is applied to any of the declaration, then the value of it cannot be changed after initialization.  Consider the below example.

Example:
const char str[] = "See-Programming";
const int num = 10;

These kind of data are stored under RO Data Segment. It is kept read only since the value cannot be altered after initialization.

Let us print the address of RO Data in our program using GDB
  (gdb) p &num
  $1 = (const int *) 0x8048510
  (gdb) p &str
  $2 = (char (*)[16]) 0x8048500

Initialized Data Segment:
It is also called as data segment.  It is available above the text/code segment.  It has static and global variables.  Initialized data segment of kept read-write, since the value of the variables can be modified during program execution.  Lifetime of the initialized data is the complete execution time of the program. Data whose lifetime is the complete execution time of the program will be mention in the executable file.  Memory for initialized data are allocated statically(static memory allocation). 

Let us print the address of the initialized data using GDB.
  (gdb) p &init_global
  $3 = (int *) 0x804a018
  (gdb) p &static_variable
  $4 = (int *) 0x804a01c

Uninitialized Data Segment:
It is also called as BSS segment.  It is placed above the data segment.  It contains uninitialized static and global variables.  And the data in BSS segment are initialized to zero.  Lifetime of uninitialized data is the complete execu

Comments

Popular posts from this blog

Difference between asterisk and ampersand operators in c

kbhit in c