0

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.

12
  • Do you have to use std::vector? Apr 5, 2013 at 14:55
  • Well, what else is it supposed to do when you call push_back and there isn't any capacity left?
    – user395760
    Apr 5, 2013 at 14:55
  • 1
    You can reserve maximum place for your vector by reserve() function. You can give maximum capacity of your vector
    – nabroyan
    Apr 5, 2013 at 14:55
  • 1
    Why do you need a pointer to an element in an array? Why not just maintain the index? Apr 5, 2013 at 14:55
  • 1
    @Sambo You haven't answered the question 'why do you need a pointer instead of an index'. If you use an index you can get the pointer whenever you need it, and the index will never be wrong even when the vector is reallocated. It does seem the obvious answer to your issue.
    – john
    Apr 5, 2013 at 15:14

3 Answers 3

2

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'.

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.

2
  • I also tried this one but this means I have to allocate dynamically every element (up to 20.000.000 and more) and this is pretty slow
    – Sambo
    Apr 5, 2013 at 15:09
  • I disagree with your evaluation.
    – Blindy
    Apr 5, 2013 at 18:02
0

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.

1
  • I tried this one. Manually defining the max number is working fine. I am able to define about 2.000.000 but when using the .max_size() member function I actually should be able to define 6.945.456 elements.
    – Sambo
    Apr 5, 2013 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.