Home > Back-end >  Why are there so many zeros in the binary file?
Why are there so many zeros in the binary file?

Time:06-07

Let me use this simple Hello world program as an example:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello world!";
}

Compile using: g hello.cpp

From quick inspection of the binary file in a text editor, it seems that about half of the resulting binary is only zeros, most of them in large blocks.

I'm not worried about it but it seems odd that the compiler would waste a bunch of space. Is there a good reason for having these large unused blocks?

CodePudding user response:

A program file consists of some metadata, executable code, read-only data, data. Each of those is aligned to the page size of your system so that they can be mapped into memory. Those "large unused blocks" are just padding to bring everything to alignment. It's only looks large because your program is basically nothing.

  • Related