简体   繁体   中英

How the memory allocation works for nested containers?

For example, i have std::vector<std::string> , how the allocators for vector and string work together?

Say the allocator for vector allocates a chunk of memory ChunkVec , does the allocator for string allocate memory inside ChunkVec so that the memory allocated for each string sums to ChunkVec ? Or the allocator for string allocates memory outside ChunkVec ?

Is the answer the same for other nested containers?

And is there a difference between C++ and C++11?

ChunkVec stores only the pointer to the data allocated by string.(in this case it stores a std::string object which stores pointer). Its a totally different allocation. A Good way to understand it is to analyze the tree structure in programming.

struct node  
{
     int data;
     struct node* left;
     struct node* right;
};

left and right are different memory allocations than node. You can remove them without removing this very node.

std::string has two things to store--the size of the string and the content. If I allocate one on the stack, the size will be on the stack as well. For short strings, the character data itself will also be on the stack. These two items make up the "control structure". std::string only uses its allocator for long strings that don't fit in its fixed-size control structure.

std::vector allocates memory to store the control structure of the std::string . Any allocation required by std::string to store long strings could be in a completely different area of memory than the vector. Short strings will be entirely managed be the allocator of std::vector .

i have std::vector < std::string >

On my Ubuntu 15.04, 64 bit, a std::string is 8 bytes, regardless of contents.

(using std::string s1; I am comparing sizeof(std::string) versus s1.size(). Then append to the string and then print them both again.)

I have not noticed or found a way to specify what allocator to use when the string allocates its data from the heap, therefore, I believe it must use some standard allocator, probably new, but I have never looked into the std::string code. And that standard allocator would know nothing about your vector.

does the allocator for string allocate memory inside ChunkVec so that the memory allocated for each string sums to ChunkVec?

I believe the part of the string in a vector element is only the 8 byte pointer to where the string 'proper' resides in the heap. So no.

Or the allocator for string allocates memory outside ChunkVec?

Yes, I believe so.

You can confirm this by printing the addresses of the vector elements i, and i+1, and the address of the some of the chars of element i.

By the way, on my implementation (g++ 4.9.2) , sizeof(std::vector) is 24 bytes, regardless of the number of data elements (vec.size()) and regardless of element size. Note also, that I have read about some implementations where some of a small vector might actually reside in the 24 bytes. Implementation details can be tedious, but helpful. Still, some might be interested in why you want to know this.


Be aware we are talking about implementation details (I think) ... so your exploration might vary from mine.


Is the answer the same for other nested containers?

I have not explored every container (but I have used many "std::vector< std::string >").

Generally, and without much thought, I would guess not.

And is there a difference between C++ and C++11?

Implementation details change for various reasons, including language feature changes. What have you tried?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM