What is the overhead cost of an empty vector?(空向量的开销成本是多少?)
问题描述
拥有一个空向量与拥有一个指向向量的指针的内存开销是多少?
What is the memory overhead of having an empty vector vs having a pointer to a vector?
选项 A:
std::vector<int> v;
选项 B:
std::vector<int> *v = NULL;
我相信选项 B 需要 1 个 32 位指针(假设这里是 32 位)空的v"占用多少内存?
I believe that option B takes 1 32 bit pointer (assuming 32 bit here) How much memory does the empty 'v' take up?
推荐答案
至于问的问题:看实现.使用 MSVC 7.1:
As for the question as asked: It depends on the implementation. With MSVC 7.1 this:
std:: cout << sizeof(std::vector<int>) << std::endl;
给我 16(字节).(3个指针:容量的开始、结束和结束,加上一个分配器)
gives me 16 (bytes). (3 pointers: begin, end, and end of capacity, plus an allocator)
然而应该注意的是,指向向量的指针给了它更大的开销:
However it should be noted that the pointer-to-vector gives it a larger overhead:
- 非空情况下的时间和空间
- 在所有情况下都很复杂.
这篇关于空向量的开销成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:空向量的开销成本是多少?
基础教程推荐
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
