为什么二进制文件中有这么多零?
让我使用此简单 Hello World 程序作为一个示例:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!";
}
使用:g ++ hello.cpp
在文本编辑器中对二进制文件的快速检查,似乎大约一半最终的二进制文件仅是零,其中大多数在大块中。
我并不担心它,但是编译器会浪费一堆空间,这似乎很奇怪。有这些大型未使用的块有充分的理由吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
程序文件由一些元数据,可执行代码,仅读取数据,数据组成。这些都与系统的页面大小对齐,以便可以将它们映射到内存中。那些“大型未使用的块”只是填充一切,以使一切保持一致。它看起来很大,因为您的程序基本上没有。
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.