Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
191 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...