0

Why does commenting out an initialized global variable loses 4 bytes of data from data segment whereas commenting out an uninitialized global variable loses 8 bytes of data from BSS segment?

PROGRAM1:

#include <stdio.h>

int global1 = 20; /* initialized global variable stored in DS*/
int global2;    /* Uninitialized global variables stored in BSS segment*/
int global3=0;  /* Global variables initialized to zero are stored in BSS*/
int main(void)
{
static int i = 100; /* Initialized static variable stored in DS*/
static int j=0;    /* Static variables initialized to 0 are stored in bss*/
 //int l=100;  /* stack stores the auto variables*/      
 int k;      /* stack stores the auto variables*/   
 return 0;
}

Data:504 BSS 32

PROGRAM2:

#include <stdio.h>
//int global1 = 20; /* initialized global variable stored in DS*/
int global2;    /* Uninitialized global variables stored in BSS segment*/
int global3=0;  /* Global variables initialized to zero are stored in BSS  segment*/
int main(void)
{
static int i = 100; /* Initialized static variable stored in DS*/
static int j=0;    /* Static variables initialized to 0 are stored in BSS  segment*/
 //int l=100;  /* stack stores the auto variables*/      
 int k;      /* stack stores the auto variables*/ 
return 0;
}

Data :500, BSS 32

PROGRAM3:

#include <stdio.h>
//int global1 = 20; /* initialized global variable stored in DS*/
//int global2;    /* Uninitialized global variables stored in BSS segment*/
int global3=0;  /* Global variables initialized to zero are stored in BSS  segment*/
int main(void)
{
static int i = 100; /* Initialized static variable stored in DS*/
static int j=0;    /* Static variables initialized to 0 are stored in BSS segment*/
 //int l=100;  /* stack stores the auto variables*/      
 int k;      /* stack stores the auto variables*/ 
 return 0;

 }

Data 500, BSS 24 (why 24 and why not 28?)

Wolf
  • 9,679
  • 7
  • 62
  • 108
JIN007
  • 165
  • 12
  • 1
    Are you sure that it's not only due to granularity? – Wolf Sep 23 '15 at 20:56
  • 1
    "Global variables initialized to zero are stored in BSS" This is not true. – too honest for this site Sep 23 '15 at 20:57
  • 3
    Look at the `map` file. – Eugene Sh. Sep 23 '15 at 21:03
  • 2
    possible duplicate of [integer variable size in bss and data segment](http://stackoverflow.com/questions/21130729/integer-variable-size-in-bss-and-data-segment) – Bo Persson Sep 23 '15 at 21:50
  • 1
    @Olaf depends on the compiler, really. – fuz Sep 23 '15 at 21:55
  • 3
    It is at the compiler's and linker's discretion what size the BSS is and which variables are assigned to it. Most importantly to the question, it is a mistake to assume that size of the BSS is exactly the sum of the sizes of all the variables assigned to it. There could easily be unused space in that section before, between, and/or after the space assigned to variables, for alignment or for any other purpose that the compiler or linker may choose. – John Bollinger Sep 23 '15 at 22:00
  • 1
    @FUZxxl: The assertion as given is false (aka "not true"), as it does not allow an alternative. IIRC, gcc has an option for this. – too honest for this site Sep 23 '15 at 23:38

0 Answers0