0

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?

9
  • 3
    Why doesn't it seem right to you that the OS would allocate memory for the program's variables? The storage for the variables is not actually inside the executable ... imagine the executable is on a read-only hard disk for example, or if it's launched twice Commented Nov 22, 2018 at 21:17
  • How are you compiling the code? How are you determining the file size? Are you looking at size on disk? It might be rounding up to a cluster size (see this explanation). Are you sure you aren't accidentally looking at the same file three times? Maybe the other examples failed to compiled or compiled to another file. Commented Nov 22, 2018 at 21:19
  • @M.M Mostly because I have noticed that more complex programs tend to have larger .exe files. Also (less sure but) because I am not sure how the program would know how much memory to allocate, for example, if I have 2 functions that each have a large static arrays and at runtime I select one of the functions to call randomly then one of the static arrays will never be instantiated. Commented Nov 22, 2018 at 21:23
  • @FrançoisAndrieux I am compiling on MSVC in Debug with default settings. You are correct that perhaps this is the problem. Yes I checked that the .exe was modified each time. Commented Nov 22, 2018 at 21:26
  • @JesperJuhl So you are saying that (in general and for more realistic examples) you would expect the size of the .exe to increase as more instructions and variables are added but this increase will not be linear depending on what optimizations the compiler can make? Commented Nov 22, 2018 at 21:27

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this seems to give me all the key concepts that I need to google to fully understand
1

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.

1 Comment

That's for the file size in Disk, but if in Memory (RAM), it's a different story

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.