10.7 – Memory segments – Code, Data, BSS

by subbu on December 5, 2013

Memory management in C language:

Memory is organized into number of segments on loading a C program. Here program may be a text editor, a compiler or an audio player etc. These segments are

  • Stack segment
  • Heap segment
  • Initialized global segment (Data segment)
  • Un-initialized global segment (BSS segment)
  • Text segment (Code segment)

Memory allocation of a C program

Text/Code segment:

The program loaded to execute is stored in the text segment, which is also called code segment. Generally it is set as read only segment to prevent from accidental modifications by other applications.
A program loaded into this segment is sharable so that single copy loaded into the memory can be used by any number of applications. Say for example dictionary component loaded in the code segment can be used by any application.

Now let us check how much size a simple C program takes in the text segment using size command in Linux.

/* hello.c */
#include<stdio.h>
int main()
{
 int x,y,z;
 x=10,y=20,z=x+y;
 printf("Sum of two numbers %d",z);
 return 0;
}

Size command in Linux

The above program takes 976 bytes in text segment.

Initialized global segment:

It is generally called as the data segment. All the global and static variables initialized by the program are stored in the Data segment. It is further classified into read only segment and read/write segments.

static and global variables, which are initialized by the program are stored in read/write portion of Data segment and their values can be changed in the program.

#include<stdio.h>
int y=25;          /* initialized global variable */
int main()
{
 static int x=45; /* initialized static variable */
 x=x+25;
 printf("x=%d",x);
 y=y+10;
 printf("\ny=%d",y);
 return 0;
}

Output:
x=70
y=35

Data segment read/write

static and global variables defined as const variables are stored in the read-only section of Data segment and initialized values of these variables can’t be changed

#include<stdio.h>
const int y=25;          /* initialized constant global variable */
int main()
{
 y=y+10;
 printf("\ny=%d",y);
 return 0;
}

Output:
In function ‘main’:
5:3: error: assignment of read-only variable ‘y’
y=y+10;
^

Data segment read only

Un-initialized global segment:

It is generally known as BSS segment, named after an ancient assembler operator Block Started by Symbol. All the static and global variables, which are not initialized by the program are stored in this segment.

These variables are initialized with zero by kernel of operating system

#include<stdio.h>
int y;          /* initialized global variable */
int main()
{
 static int x; /* initialized static variable */
 printf("x=%d",x);
 printf("\ny=%d",y);
 return 0;
}

Output:
x=0
y=0

BSS segment in C

Let us test some cases using the size command of Linux. The size command gives the size of text, data and BSS segments.

Here the program name is “demo.c”

#include<stdio.h>
int main()
{
 return 0;
}
[codingfox@codingfox-laptop]$ gcc -o demo demo.c
[codingfox@codingfox-laptop]$ size ./demo
text       data        bss        dec        hex    filename
960        248          8        1216        4c0    demo.c

Let us add one global variable “x” in the program and check the size of BSS

#include<stdio.h>
int x; /* Uninitialized variable stored in bss*/
int main()
{
 return 0;
}
[codingfox@codingfox-laptop]$ gcc -o demo demo.c
[codingfox@codingfox-laptop]$ size ./demo
text       data        bss        dec        hex    filename
960        248         12        1220        4c4    demo.c

Here the size of BSS is incremented by 4 as un-initalized global variable is stored in BSS.
Let us add one static variable to the program.

#include<stdio.h>
int x; /* Uninitialized variable stored in bss*/
int main()
{
 static int y; /* Uninitialized static variable stored in bss */
 return 0;
}
[codingfox@codingfox-laptop]$ gcc -o demo demo.c
[codingfox@codingfox-laptop]$ size ./demo
text       data        bss        dec        hex    filename
960        248         16        1224        4c8     demo.c

Here the size of BSS further incremented by 4 as the static un-initalized variable is also stored in BSS
If we initialize the static variable then it is stored in Data segment

#include<stdio.h>
int x; /* Uninitialized variable stored in bss*/
int main()
{
 static int y=45; /* Initialized static variable stored in DS*/
 return 0;
}
[codingfox@codingfox-laptop]$ gcc -o demo demo.c
[codingfox@codingfox-laptop]$ size ./demo
text       data        bss        dec        hex    filename
960         252         12       1224        4c8     demo.c

Here, the size of BSS is reduced by 4 and increased the size of Data by 4 because the static variable is stored in Data segment rather in BSS.
Same way, if we initialize the global variable then it is stored in the Data segment instead in BSS

#include<stdio.h>
int x=20; /* initialized global variable stored in Data segment*/
int main()
{
 static int y=45; /* Initialized static variable stored in Data segment*/
 return 0;
}
[codingfox@codingfox-laptop]$ gcc -o demo demo.c
[codingfox@codingfox-laptop]$ size ./demo
text       data        bss        dec        hex    filename
960        256          8        1224        4c8    demo.c

Previous post:

Next post: