What is the overhead cost of an empty vector?

后端 未结 6 1525
野性不改
野性不改 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:46

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

    std:: cout << sizeof(std::vector) << 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.

提交回复
热议问题