开发者

What happens when you increase the size of a struct in a vector?

A problem I'm working on involves a large tree structure. Initially I was creating the tree by newing new nodes and attaching them to their parent node and such. This was taking an awful long time. A friend suggested instead I forgo the dynamic memory allocation and structure the tree as an array with offsets to child words. I've not done this before and so I have some questions about what actually happens.

Here's the ultra basic, no safety checks on anything example:

struct DataStructure
{
    std::vector<Entry> mCollection;
};

struct Entry
{
    char mValue;
    std::vector<unsigned int> mOffsetCollection; //a vector of indexes that are offsets to other entries.
};

I'm curious about what happens as I add more data to this structure. If I added

DataStructure d;
Entry entry;
entry.mValue = 'a';
d.push_back(entry);
.
.
.//add some more entries...
.
.
//now suppose I add a bunch of offsets to these various entries in my array.
Entry& firstEntry = d.at(0);
firstEntry.mOffsetCollection.push_back(4);
firstEntry.mOffsetCollection.push_back(9);
firstEntry.mOffsetCollection.push_back(32);
..

So the size of this first Entry is increasing. What exactly happens? I just ran through through a small example and it seemed to work fine. The other entries in the data struc开发者_运维知识库ture were unaffected. I was initially concerned that perhaps if the size of the structure became large it would run into the next item in the array but I suppose that isn't happening. It makes me realize that I don't really know what is going on behind the scenes. Is the vector<Entry> in DataStructure d having to reallocate memory?


Yes, a std::vector dynamically manages its own memory.

The size of a struct (indeed, any data type) is fixed at compile time; it is not affected at run-time.


You're not using dynamic allocation, but the vector class is. The internal vector<int> is storing its variable-length array externally to the Entry object that you're putting in the outer vector.


The size of a std::vector is constant, as it stores data using pointers to heap blocks internally, generally it has a begin, end and current pointer, and not much else, making it pretty small as well.


This might seem nit-picky, but your choice of verbiage threw me off and I suspect it might be throwing you off as well...

What happens when you increase the size of a struct in a vector?

Keep in mind that the size of the struct itself never changes, nor can any struct ever change in size at runtime.

If you do sizeof(DataStructure) at the beginning of the program, then push a bunch of stuff back and do sizeof(DataStructure) again, the results will be exactly the same. This is because the data you're pushing back isn't part of DataStructure iself, but is pointed to by something in DataStructure.

Where are the pointers I'm talking about? In the vector. vector also never changes size -- but the number of items within it does. vector manages a pointer to some dynamically-allocated array of stuff that you're pushing back. As you push back more stuff, the managed array fills up. When the managed array is finally full to capacity(), the vector has to allocate a new, bigger array and copy your stuff in to it (which is why STL-contained objects must be "copyable").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜