0

A highly voted previous answer's highly voted comment states:

consider having many uninitialized buffers 4096 bytes in length. Would you want all of those 4k buffers to contribute to the size of the binary? That would be a lot of wasted space.

I am building the following two files into an executable on ubuntu:

main.c

int sum(int *a, int n);

int array[2] = {1,2};

int abc;//Comment in case (a) Uncomment in case (b) and (c)
int def;//Comment in case (a) and (b) Uncomment in case (c)

int main(){
    int val = sum(array, 2);
    return val;
}

sum.c

int sum(int *a, int n){
    int i, s = 0;
    for(i = 0; i < n; i++)
        s += a[i];
    return s;
}

The following command is used to create the executable

$gcc -Og -o prog main.c sum.c

There are 3 cases:

(a) has no uninitialized global variable. The executable has size 8648 bytes.

(b) has uninitialized global variable abc. The executable has size 8680 bytes.

(c) has uninitialized global variables abc and def. The executable has size 8704.

My question is, why does the executable size even change? My understanding (also confirmed by the answer linked to above) was that uninitialized global variables should NOT affect executable size.

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • Look in the linker map to see which parts of the executable changed – M.M Dec 01 '18 at 09:39
  • @Tryer: What happens without the Og option? – P.W Dec 01 '18 at 10:54
  • @P.W no change whatsoever. i.e., case c still has 8704 bytes, case a, 8648. – Tryer Dec 01 '18 at 11:07
  • 1
    @Tryer: Maybe you should optimize for size (`-Os`) and see what happens. If it is still the same then it has nothing to do with these variables. Did you try putting the global variables in sum.c and see what happens? – P.W Dec 01 '18 at 11:29

0 Answers0