为什么C ++的初始分配比C大得多? [英] Why is C++ initial allocation so much larger than C's?

查看:67
本文介绍了为什么C ++的初始分配比C大得多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用相同的代码时,只需更改编译器(从C编译器到C ++编译器)将更改分配的内存量.我不太确定为什么会这样,并且想进一步了解它.到目前为止,我得到的最好的答复是可能是I/O流",它的描述性不是很强,这使我对C ++的不用付钱,不用付钱"感到疑惑./p>

我正在使用分别为7.0.1-8和8.3.0-6的Clang和GCC编译器.我的系统在最新的Debian 10(Buster)上运行.基准测试是通过Valgrind Massif完成的.

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}

所使用的代码不会更改,但是无论我是以C还是C ++进行编译,它都会更改Valgrind基准测试的结果.但是,这些值在编译器之间保持一致.该程序的运行时分配(峰值)如下:

  • GCC(C):1,032字节(1 KB)
  • G ++(C ++):73,744字节(〜74 KB)
  • C声(C):1,032字节(1 KB)
  • Clang ++(C ++):73,744字节(〜74 KB)

对于编译,我使用以下命令:

clang -O3 -o c-clang ./main.c
gcc -O3 -o c-gcc ./main.c

clang++ -O3 -o cpp-clang ./main.cpp
g++ -O3 -o cpp-gcc ./main.cpp

对于Valgrind,我在每种编译器和语言上运行valgrind --tool=massif --massif-out-file=m_compiler_lang ./compiler-lang,然后在ms_print上显示峰.

我在这里做错什么了吗?

解决方案

堆用法来自C ++标准库.它在启动时分配内存供内部库使用.如果您不反对它,则C和C ++版本之间的差应该为零.使用GCC和Clang,您可以使用以下命令编译文件:

g++ -Wl,--as-needed main.cpp

这将指示链接器不要链接未使用的库.在您的示例代码中,未使用C ++库,因此它不应链接到C ++标准库.

您也可以使用C文件进行测试.如果您使用以下命令进行编译:

gcc main.c -lstdc++

即使您已经构建了C程序,也会重新显示堆使用情况.

堆的使用显然取决于您正在使用的特定C ++库实现.在您的情况下,这就是GNU C ++库, libstdc ++ .其他实现可能不会分配相同数量的内存,或者可能根本不分配任何内存(至少在启动时不分配).LLVM C ++库( libc ++ )至少在我的Linux计算机上不会在启动时进行堆分配:

clang++ -stdlib=libc++ main.cpp

堆的使用与根本没有针对它的链接相同.

(如果编译失败,则可能未安装libc ++.程序包名称通常包含"libc ++"或"libcxx".)

When using the same code, simply changing the compiler (from a C compiler to a C++ compiler) will change how much memory is allocated. I'm not quite sure why this is and would like to understand it more. So far the best response I've gotten is "probably the I/O streams", which isn't very descriptive and makes me wonder about the "you don't pay for what you don't use" aspect of C++.

I'm using the Clang and GCC compilers, versions 7.0.1-8 and 8.3.0-6 respectively. My system is running on Debian 10 (Buster), latest. The benchmarks are done via Valgrind Massif.

#include <stdio.h>

int main() {
    printf("Hello, world!\n");
    return 0;
}

The code used does not change, but whether I compile as C or as C++, it changes the results of the Valgrind benchmark. The values remain consistent across compilers, however. The runtime allocations (peak) for the program go as follows:

  • GCC (C): 1,032 bytes (1 KB)
  • G++ (C++): 73,744 bytes, (~74 KB)
  • Clang (C): 1,032 bytes (1 KB)
  • Clang++ (C++): 73,744 bytes (~74 KB)

For compiling, I use the following commands:

clang -O3 -o c-clang ./main.c
gcc -O3 -o c-gcc ./main.c

clang++ -O3 -o cpp-clang ./main.cpp
g++ -O3 -o cpp-gcc ./main.cpp

For Valgrind, I run valgrind --tool=massif --massif-out-file=m_compiler_lang ./compiler-lang on each compiler and language, then ms_print for displaying the peaks.

Am I doing something wrong here?

解决方案

The heap usage comes from the C++ standard library. It allocates memory for internal library use on startup. If you don't link against it, there should be zero difference between the C and C++ version. With GCC and Clang, you can compile the file with:

g++ -Wl,--as-needed main.cpp

This will instruct the linker to not link against unused libraries. In your example code, the C++ library is not used, so it should not link against the C++ standard library.

You can also test this with the C file. If you compile with:

gcc main.c -lstdc++

The heap usage will reappear, even though you've built a C program.

The heap use is obviously dependant to the specific C++ library implementation you're using. In your case, that's the GNU C++ library, libstdc++. Other implementations might not allocate the same amount of memory, or they might not allocate any memory at all (at least not on startup.) The LLVM C++ library (libc++) for example does not do heap allocation on startup, at least on my Linux machine:

clang++ -stdlib=libc++ main.cpp

The heap use is the same as not linking at all against it.

(If compilation fails, then libc++ is probably not installed. The package name usually contains "libc++" or "libcxx".)

这篇关于为什么C ++的初始分配比C大得多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆