空向量的开销成本是多少?
空向量与指向向量的指针相比,内存开销是多少?
选项A:
std::vector<int> v;
选项B:
std::vector<int> *v = NULL;
我相信选项B采用1个32位指针(假设这里是32位) 空的“v”占用多少内存?
What is the memory overhead of having an empty vector vs having a pointer to a vector?
Option A:
std::vector<int> v;
Option B:
std::vector<int> *v = NULL;
I believe that option B takes 1 32 bit pointer (assuming 32 bit here)
How much memory does the empty 'v' take up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
至于所问的问题:这取决于实施。 对于 MSVC 7.1,此:
给我 16(字节)。 (3个指针:容量的开始、结束和结束,加上一个分配器)
但是应该注意的是,指向向量的指针给它带来了更大的开销:
As for the question as asked: It depends on the implementation. With MSVC 7.1 this:
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:
它完全依赖于实现,您不应假设或依赖细节。 对于使用 VC 的 20 字节而言,它是值得的。
It's completely implementation-dependent and you should neither assume nor rely on the details. For what it's worth it's 20-bytes using VC.
std::vector v;
占用sizeof(v)
空间。 它可能因实现而异,因此运行它并了解它需要多少时间。std::vector v;
takes upsizeof(v)
space. It might vary by implementation, so run it and find out how much it takes for you.VS2005:
谢谢!
VS2005:
Thanks!
在 Visual Studio Community 2017(版本 15.2)中,运行以下代码:
以 32 位 (x86) 运行,我获得向量 16 个字节和向量指针 4 个字节。
在 64 位 (x64) 下运行,我得到 32 个字节的向量和 8 个字节的向量指针。
In Visual Studio Community 2017 (Version 15.2), running this code:
Running in 32 bit (x86), I get 16 bytes for the vector and 4 bytes for the vector pointer.
Running in 64 bit (x64), I get 32 bytes for the vector and 8 bytes for the vector pointer.
取决于实现,可能是一个指针和两个表示当前大小和容量的整数。
Implementation dependant, probably a pointer and two integers for current size and capacity.