Reputation: 117
I started with an "empty" program and checked the size of the .exe file produced
int main()
{
system("pause");
}
Exe Size: 58.5 KB (59,904 bytes)
I then added a large array of static variables
int main()
{
const int BIG_NUMBER = 40000000;
static int x[40000000];
system("pause");
}
Exe Size: 58.5 KB (59,904 bytes)
Making the array non-static also had no effect. I added some code to (a) make 100% sure the variable is not being optimised away and (b) see if the extra instructions would increase the number of bytes of the .exe
int main()
{
const int BIG_NUMBER = 40000000;
static int x[40000000];
for (int i = 0; i < BIG_NUMBER; ++i)
{
std::cout << x[i] << std::endl;
}
system("pause");
}
Exe Size: 58.5 KB (59,904 bytes)
Literally not a single byte more. At this point my (stab in the dark) guess is that the .exe requests the OS to allocate the correct amount of memory needed for the static variables when the program starts but this doesn't seem right. What determines the .exe file size?
Upvotes: 0
Views: 543
Reputation: 386
What determines the .exe file size?
It really depends on what you're making actually. But the most notable so far is the library you included in #include
Making the array non-static also had no effect. I added some code to (a) make 100% sure the variable is not being optimized away and (b) see if the extra instructions would increase the number of bytes of the .exe
Did you enable debug mode? Or are you using release mode? Be careful, because in release mode, they'll optimize your code too much
.
int main()
{
const int BIG_NUMBER = 40000000;
static int x[40000000];
for (int i = 0; i < BIG_NUMBER; ++i)
{
std::cout << x[i] << std::endl;
}
system("pause");
}
Your code above only contains "simple" instructions, thus, the file size wont increase.
Oh yeah, also a note, try using std::cin.get()
instead of system("PAUSE")
If you look into the assembly's perspective, all your code does is this :
allocate 4 bytes of memory at 0xRANDOM address for variable BIG_INT
allocate another 40000000 bytes of memory at 0xRANDOM+4bytes address for variable x
create variable i = 0
create goto address "loop"
output x[i] // This is also a problem, because x = null!
increment into i by one
if i is less than BIG_INT then jump to "loop"
Which is very simple to a computer. Try creating lots of functions and objects, then, you may start to see a difference.
Upvotes: 1
Reputation: 182829
I compiled your program with and without the large array, dumped both section headers and compared them. This is the only difference:
Idx Name Size VMA LMA File off Algn
- 23 .bss 09896820 0000000000004020 0000000000004020 00003010 2**5
+ 23 .bss 00000008 0000000000004010 0000000000004010 00003010 2**0
As you suspected, the only size difference between the two is the size of the block of memory that the executable requests from the OS. There's may be an extra byte or two of code, but likely the code is rounded up to the nearest page anyway.
As the linked page states:
Typically only the length of the bss section, but no data, is stored in the object file. The program loader allocates memory for the bss section when it loads the program.
Upvotes: 6