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.