为什么二进制文件中有这么多零?

发布于 2025-02-04 14:46:10 字数 353 浏览 2 评论 0原文

让我使用此简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懷念過去 2025-02-11 14:46:10

程序文件由一些元数据,可执行代码,仅读取数据,数据组成。这些都与系统的页面大小对齐,以便可以将它们映射到内存中。那些“大型未使用的块”只是填充一切,以使一切保持一致。它看起来很大,因为您的程序基本上没有。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文