简体   繁体   中英

c++ vector avoid reallocation by using .push_back or .resize()

I have a question related to vector especially the option .push_back() and .resize(). When using this option c++(STL) will always reallocate every element when the current vector capacity is overstep. This is a problem for me, because I do have a vector of struct objects.

std::vector<node> v;

My struct is looking like this and keeps pointer to other elements of the vector

struct node
{
    std::array<node*, nrOfNeigh> neighb;
    node* parentNode; 
    double density;
    ...
}

Because my struct do have pointer to other elements of the vector, always when using .push_back() this dependency will not be valid anymore.

Do any of you have an idea to avoid this?

I do not expect that there is a way to force std::vector not to reallocate. I already tried to use .reserve() and thereby reserved as much is possible. this is possible but from the memory management point of view not nice.

Assuming that the node* fields in the struct only point to other 'node' objects in the vector, you can replace node* with an integral index into the vector.

Specifically,

struct node
{
    std::array<size_t, nrOfNeigh> neighb;
    size_t parentNodeId;
    double density;
    ...
}

Now, when you use push_back(), instead of storing &v.back(), you store 'v.size()-1'.

Personally I would use a std::vector<node *> (or any of the countless smart pointer implementations in Boost), so you only reallocate the pointers to nodes, not the nodes themselves.

Don't forget to release the pointers if you're going the non-smart pointer route though.

Edit: Especially if you're holding entire arrays inside your struct. You don't want to go reallocating every fixed array every reallocation cycle. I would also not use fixed arrays, an inner std::vector<node *> to hold neighbours would scale better and avoid most of the buffer overflow problems software these days seems plagued with.

Well you could use the .reserve() functionality of vector to allocate as much memory as you think you might need before you use the vector, thereby possibly avoiding the re-allocations. It can also speed up vector pushes in some instances. See here.

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