What is the overhead cost of an empty vector?

后端 未结 6 1523
野性不改
野性不改 2020-12-06 04:24

What is the memory overhead of having an empty vector vs having a pointer to a vector?

Option A:

std::vector v;

Option B

6条回答
  •  悲&欢浪女
    2020-12-06 04:55

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

    #include 
    #include 
    
    using namespace std;
    
    void main()
    {
        vector test;
        vector* 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.

提交回复
热议问题