Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Russell

std::vector and memory allocation

This topic is 5228 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Normally if I have a vector and use it as a stack, and the stack grows to, say, 100 elements, then the stack shrinks to 30 elements, there is still memory allocated for 100 elements. I think that''s right. Okay, so now if I have a struct that contains a vector, and I make a vector of that struct, I''m guessing the vectors in the structs do not retain their allocation. Is that right? For example...
struct Foo
{
    std::vector<Bar> bars;
    // other stuff...

};

std::vector<Foo> foos;

// each Foo''s ''bars'' vector gets, say, 15 things added to it below

push_back_20_foos(foos); // now 20 things in foos

pop_back_10_foos(foos);  // now 10 things in foos, but still allocation for 20 foos
Now if I push_back() another Foo, and then add things to the bars vector of that Foo, there will be allocation of the Bars, right? The allocated memory for the Foo will already be there, but not for the Bar, right? The bars vector doesn''t retain a capacity after it''s popped, right? Any way I can get this functionality? If this is confusing I''ll try to explain it better.

Share this post


Link to post
Share on other sites
Advertisement
It is confusing

A std::vector is just a fancy pointer to some dynamically allocated array. That dynamic array never shrinks during the livetime of a std::vector. It doesn''t matter if the std::vector is contained in a class or not.

You can shrink a std::vector by clever usage of std::vector::swap...

Share this post


Link to post
Share on other sites
I guess what I''m wanting is that when I pop_back() a Foo from ''foos'', I don''t want the ''bars'' vector of that Foo to get deallocated, since another Foo of a very similar size will get reallocated via a push_back() very shortly.

Share this post


Link to post
Share on other sites
If you have a structure containing a vector, and that structure goes out of scope (e.g. you pop it), then the vectors destructor gets called, and it frees the memory.
If you don't want that to happen, you could try adding a flag to the struct, indicating if its free or not, and changing that flag instead of popping the structure. Personally, i wouldn't worry about it - STL (I use STLPort) does some of its own memory allocation, to increase speed and decrease heap fragmentation. So allocating memory won't be that slow.

If you need this to be speed critical, you might want to look at a custom stl allocator, or doing things differently - for example storing a vector of pointers to foos, and managing the foos in some other way (e.g. allocate 100 foos, and then return one of them when you need)

[edited by - Evil Steve on February 26, 2004 8:34:16 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!