- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
考虑以下测试程序:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::cout << sizeof(std::string("hi")) << " ";
std::string a[10];
std::cout << sizeof(a) << " ";
std::vector<std::string> v(10);
std::cout << sizeof(v) + sizeof(std::string) * v.capacity() << "\n";
}
libstdc++
和 libc++
的输出分别为:
8 80 104
24 240 264
如您所见,libc++
占用的内存是简单程序的 3 倍。导致这种内存差异的实现有何不同?我需要担心吗?如何解决?
最佳答案
这是一个简短的程序,可帮助您探索 std::string
的两种内存使用情况:堆栈和堆。
#include <string>
#include <new>
#include <cstdio>
#include <cstdlib>
std::size_t allocated = 0;
void* operator new (size_t sz)
{
void* p = std::malloc(sz);
allocated += sz;
return p;
}
void operator delete(void* p) noexcept
{
return std::free(p);
}
int
main()
{
allocated = 0;
std::string s("hi");
std::printf("stack space = %zu, heap space = %zu, capacity = %zu\n",
sizeof(s), allocated, s.capacity());
}
使用 http://melpon.org/wandbox/很容易获得不同编译器/库组合的输出,例如:
gcc 4.9.1:
stack space = 8, heap space = 27, capacity = 2
gcc 5.0.0:
stack space = 32, heap space = 0, capacity = 15
clang/libc++:
stack space = 24, heap space = 0, capacity = 22
VS-2015:
stack space = 32, heap space = 0, capacity = 15
(最后一行来自http://webcompiler.cloudapp.net)
上面的输出还显示了 capacity
,它衡量了字符串在必须从堆中分配一个新的、更大的缓冲区之前可以容纳多少个 char
.对于 gcc-5.0、libc++ 和 VS-2015 实现,这是 短字符串缓冲区 的度量。即在栈上分配大小缓冲区来保存短字符串,从而避免了更昂贵的堆分配。
似乎 libc++ 实现具有最小的(堆栈使用)短字符串实现,但包含最大的短字符串缓冲区。如果计算 total 内存使用量(堆栈 + 堆),libc++ 在所有 4 种实现中对于这 2 个字符的字符串的总内存使用量最小。
需要注意的是,所有这些测量都是在 64 位平台上进行的。在 32 位上,libc++ 堆栈使用量将下降到 12,小字符串缓冲区下降到 10。我不知道 32 位平台上其他实现的行为,但您可以使用上面的代码找出.
关于c++ - 为什么 libc++ 的 std::string 实现占用 3 倍于 libstdc++ 的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27631065/
我看到这两个项目非常相关,但它们之间有什么区别?官方网页并没有说明太多。 我知道ABI(Application Binary Interface)用于在不同平台之间提供低级二进制接口(interfac
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
现代 glibc 二进制文件在 Linux 中称为 libc.so.6。为什么这里用“6”? libc.so.1 或 libc.so.8 也可以是好名字恕我直言。 维基百科在 http://en.wi
我在我的 Linux Mint 14 Nadia 中安装了 Matlab(a uname -a 显示:Linux Ideapad-Z570 3.5.0-17-generic#28-Ubuntu SMP
我有一个使用 eventfd 和 timerfd 内核系统调用的应用程序。为此,您需要一个现代内核和一个支持它们的 libc,至少是 2.8。 我目前的情况是,我的系统具有正确的内核,但是 2.7.1
我有一个用 musl libc 编译的共享库 $ readelf -d ./libinterop_d.so Dynamic section at offset 0x8ecb0 contains 22
我的问题是,是否总是需要 libc.a,即使我希望链接到 libc.so。我搜索了互联网,它给出了 3 个可能的答案(冲突) 1. c program will require to link to
我安装了多个版本的 libc,我如何在编译时选择要链接的对象? 现在我正在编译 g++ prog.cpp 最佳答案 您的程序将链接到 libc。当您在 gdb 中运行时,libc-dbg 将用于读取符
我在 Ubuntu 16.04 64 位上使用 gcc 5.4.0。当我编译程序时: gcc -o prog prog.c GCC 会自动链接到 C 标准库,因此我不必专门这样做。 我如何查看 gcc
我正在为我的系统安全类编写返回 libc 攻击。一、漏洞代码: //vuln.c #include #include int loadconfig(void){ char buf[1024];
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我正在尝试查找信息,但我没有在 clang 网站上看到它。我想尝试在 Windows 上使用它,但我不知道它是有自己的 libc 还是使用 MS 损坏的 libc? 另一个问题:如果我用 clang
我正在使用 clang 的规定通过插件和 clang 的 LibTooling 开发插件和工具。我能够做以下事情: 从 svn(Linux 和 OSX),通过运行配置脚本(不使用 cmake)按照入门
尝试运行时出现以下错误。 lib/libc.so.6:找不到(test.so)所需的版本“GLIBC_2.7” 要解决这个问题,在编译时静态链接到 libc-2.11.a 是否安全,这样在运行时就不会
首先,我在谷歌上搜索了很多,但没有找到与我的案例相关的任何东西,我有一个ELF可执行文件,我试图在我的Ubuntu WSL中运行它,我已经更改了权限(chmod+x文件),当我运行它时,显示以下错误。
GNU page说: Your program can arrange to run its own cleanup functions if normal termination happens.
我正在尝试在我的路由器上交叉编译 MIPSEL。我在汇编中找到了一些东西,但现在我开始尝试编译基本的 C 代码。 目前只有简单的 hello world c 代码,并使用 mipsel-linux-g
libc 中预期的字符编码是什么?例如,gethostname(char name, size_t namelen);以 char 作为参数。名称参数是否应该以 utf8(保持 ascii 完整)或纯
题 如果我包括 time.h ,更改“源类型参数”并重新包含该 header ,难道不应该添加这些新定义吗?我知道这是由于包括 guard 而发生的。我的问题是:这是 libc 中的错误吗?难道它不能
我正在学习Ubuntu上的动态链接器。我想导入我需要的libc函数,但是它链接了while libc.so文件。我不知道该怎么做,所以我来这里寻求帮助。 最佳答案 如果您将程序链接到动态库(例如 li
我是一名优秀的程序员,十分优秀!