Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::vector memory question

Started by Lode Oct 15, 2005 at 5:48 AM 3 replies 400+ views
Original Post
Lode
Lode
If you do the following:

void bla(std::vector<int> &v)
{
    std::vector<int> w;
    for(int i = 0; i < 100; i++) w.push_back(i);
    v = w;
}

int main(...)
{
    std::vector<int> a;
    bla(a);
}

At the end of the main function, will a have size 100, and will it contain values 0 through 99? In the bla function, w is created and filled with values 0 through 99. Then v is set to w. Is the memory of w copied over to v, or do v and w share the same memory? And when the function bla is done and we're back in main, w has gone out of scope, so if the memory of w is then destroyed, what happens to that of a?
Scet
Scet
Well after testing it, at the end of main "a" had 100 entries from 0-99. My guess is that the = operator on vector calls std::copy or something. Changing the type of "w" creates an error so I don't think it's not just taking the address.

Edit: I don't why you need "w" at all since you pass "a" to the function. I also don't know why you couldn't test it yourself.
Cocalus
Cocalus
The memory is copied.

After bla is done a holds a copy of what was once in w.

If you don't want to copy the memory you can use v.swap(w), (v now holds what w did, and vice versa) instead of v=w. I think this is (alot) faster with vectors. I know swap is faster than copying with maps and lists. Another option is to v.clear() then just use v instead of a temporaray. There's a bunch of other ways to do this too.

Edit: Ya swap is much faster. In gcc It's just swaping 3 pointers.
Lode
Lode
Quote:
Original post by ScetEdit: I don't why you need "w" at all since you pass "a" to the function. I also don't know why you couldn't test it yourself.


This was just a simple example, in my own program an image is loaded from HD and stored in an std::vector and then another std::vector of a texture is set to this std::vector. I could test it myself, but there are two reasons why I still had to ask this question:

-When testing it, I can't see if the memory is copied or not.
-a indeed has the contents of w, but I'm not sure if this is compiler dependent or not.

Since I now know the memory is copied, the second thing is not an issue anymore :)

Thanks for the answers!
snk_kid
snk_kid
Quote:
Original post by Lode
... but I'm not sure if this is compiler dependent or not.


This has relatively nothing to do with compiler depedancy, all standard library containers are value types, they implement proper copy semantics. The standard defines a set of *Concept* some of which are refinements of other Concepts, each container is a model of a particular kind of Concept, at the very top level all containers are a model of the Assignable Concept which defines copy/value semantics. I recommend reading about the Concepts here.

Concepts are like interfaces but are not signature-oriented, they do not have any cost in preformance themselfs and they are much richer.

[Edited by - snk_kid on October 15, 2005 11:11:17 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.