Memory consumption of a pointer to vector of pointers

前端 未结 5 2089
有刺的猬
有刺的猬 2021-01-26 09:28

I am aware that the size of a pointer is fixed (not the size of the data it points to). Now given that, supposing I have a vector of data in global scope and I declare a pointer

5条回答
  •  [愿得一人]
    2021-01-26 09:57

    Re: I am hoping that the additional memory consumption would be just the size of vector times the fixed size of a pointer (say, 8 bytes) regardless of the complexity of the data; since the data it points to exists in global scope (i.e., no new data is being allocated; hope I've explained my thoughts clearly).

    You are basically right. For any reasonable implementation of std::vector, the underlying storage for a std::vector is just a compact array of T. In this case, your T is the pointer type data * and not data.

    There may be some extra storage for efficient expansion, oterhwise every push_back operation would have to grow the array. (Take a look a the reserve and capacity functions of std::vector.)

    And of course there is some small overhead for allocating the vector object itself.

提交回复
热议问题