空向量的开销成本是多少?

发布于 2024-07-13 17:19:50 字数 219 浏览 5 评论 0原文

空向量与指向向量的指针相比,内存开销是多少?

选项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 技术交流群。

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

发布评论

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

评论(6

何必那么矫情 2024-07-20 17:19:50

至于所问的问题:这取决于实施。 对于 MSVC 7.1,此:

std:: cout << sizeof(std::vector<int>) << std::endl;

给我 16(字节)。 (3个指针:容量的开始、结束和结束,加上一个分配器)

但是应该注意的是,指向向量的指针给它带来了更大的开销:

  • 在非 空的。
  • -所有情况下的复杂性都是

As for the question as asked: It depends on the implementation. With MSVC 7.1 this:

std:: cout << sizeof(std::vector<int>) << std::endl;

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:

  • in both time and space in the non-empty case
  • in complexity in all cases.
背叛残局 2024-07-20 17:19:50

它完全依赖于实现,您不应假设或依赖细节。 对于使用 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.

愿得七秒忆 2024-07-20 17:19:50

std::vector v; 占用 sizeof(v) 空间。 它可能因实现而异,因此运行它并了解它需要多少时间。

std::vector v; takes up sizeof(v) space. It might vary by implementation, so run it and find out how much it takes for you.

一场春暖 2024-07-20 17:19:50

VS2005:

std::vector<int> *ptrToVec = new std::vector<int>();
std::vector<int> vecOfInt;

sizeof(ptrToVec) = 4
sizeof(vecOfInt) = 20

谢谢!

VS2005:

std::vector<int> *ptrToVec = new std::vector<int>();
std::vector<int> vecOfInt;

sizeof(ptrToVec) = 4
sizeof(vecOfInt) = 20

Thanks!

别念他 2024-07-20 17:19:50

在 Visual Studio Community 2017(版本 15.2)中,运行以下代码:

#include <iostream>
#include <vector>

using namespace std;

void main()
{
    vector<float> test;
    vector<float>* test2 = &test;
    cout << sizeof(test) << "\n";
    cout << sizeof(test2) << "\n";

    cout << "\n";
    system("pause");
}

以 32 位 (x86) 运行,我获得向量 16 个字节和向量指针 4 个字节。

在 64 位 (x64) 下运行,我得到 32 个字节的向量和 8 个字节的向量指针。

In Visual Studio Community 2017 (Version 15.2), running this code:

#include <iostream>
#include <vector>

using namespace std;

void main()
{
    vector<float> test;
    vector<float>* test2 = &test;
    cout << sizeof(test) << "\n";
    cout << sizeof(test2) << "\n";

    cout << "\n";
    system("pause");
}

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.

无畏 2024-07-20 17:19:50

取决于实现,可能是一个指针和两个表示当前大小和容量的整数。

Implementation dependant, probably a pointer and two integers for current size and capacity.

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