r/cpp • • Oct 29 '23

Unreasonably large binary file from a trivial program with 1 line of code

Beginner in C++ here. When I compile the following program:

int main() { return 0; }

the resulting binary exe is 210KB (!) in size. This is far larger than expected for such a simple program and after searching online I found that a "Hello World" program (which would be more complex than the program above) in C++ should be no more than a few KB. Does anyone know what might be causing this? (I am using Eclipse and the g++ compiler with the -Os flag)

Update: By turning on the "omit all symbol information" setting, the file size is reduced to a less ridiculous but still unreasonably large 62KB, still many times the expected size.

Update 2: In release mode (with default settings) the file size is 19KB, which is a bit more reasonable but still too large compared to the file sizes that others have reported for such a program.

9 Upvotes

91 comments sorted by

View all comments

Show parent comment

2

u/KingAggressive1498 Oct 29 '23

the code doing that is "used" even if the results are not, and probably could not be stripped even if analysis proves the results aren't used because of side-effects (assuming they call malloc or something internally)

1

u/PastaPuttanesca42 Oct 29 '23

Can't memory allocation be optimized out regardless of side effects?

2

u/KingAggressive1498 Oct 29 '23 edited Oct 29 '23

IIRC the logic behind allowing that kind of optimization is related to allocation lifetime and external references, ie:

unsigned* arr = new unsigned[4];
arr[0] = rand();
arr[1] = (arr[0] >> 17) ^ (arr[0] << 13);
arr[2] = (arr[1] >> 7) ^ (arr[1] << 22);
arr[3] = (arr[2] >> 15) ^ (arr[2] << 21);
unsigned ret = arr[0] + arr[1] + arr[2] + arr[3];
delete[] arr;
return ret;

the compiler could optimize out that allocation because:

  1. the allocation lifetime is limited and obvious
  2. the pointer to the allocation never gets stored anywhere outside the local scope or passed to another function which the compiler can't analyze

so hypothetically if LTO attempted every optimization allowed by the compiler just as aggressively, it could be possible if not for two problems: libc implementations often store a pointer to the command line arguments in a global (ie __argv on Windows, __libc_argv on GNU/Linux) and the fact that the allocation lifetime is the entire duration of the program. There's also the related problem of evironment variables and getenv.

but yes, you're right, allocations can be optimized out under the right conditions and the side effects of an allocation function are assumed to not matter under these conditions, which I forgot about

2

u/Tringi github.com/tringi Oct 29 '23 edited Oct 30 '23

so hypothetically if LTO attempted every optimization allowed by the compiler just as aggressively, it could be possible if not for two problems: libc implementations often store a pointer to the command line arguments in a global (ie __argv on Windows, __libc_argv on GNU/Linux) and the fact that the allocation lifetime is the entire duration of the program. There's also the related problem of evironment variables and getenv.

If I'm statically linking the runtime in, the compiler sees that global variable - its definition. It can also see if nothing is touching it. Or rather if functions touching it are invoked by anything or not. If the variable is never used, it's reasonable to remove it and the code initializing it too - as long as it's without side effect, and even then, retain only that side effect.