std::vector Question

Started by
11 comments, last by SiCrane 11 years, 8 months ago
Hello.
I am currently using vectors.
But i am unsure do i need to call myVector.clear before my application ends?

int main()
{
std::vector<int> tempVec;
int a = 55;
int b = 12;
tempVec.push_back(a);
tempVec.push_back(b);

//Do i need to call .clear at my tempVec here?
tempVec.clear()
//Or is it done automaticly at end of program?
return 0;
}
Advertisement
The content of the vector is automatically released when the vector is destroyed.
A stack allocated vector will automatically free its contents when it goes out of scope. In this case, you don't need to call clear().

A stack allocated vector will automatically free its contents when it goes out of scope. In this case, you don't need to call clear().

Thanks for quick replay and clear answer.
To add some detail to avoid some possible 'gotchas' you might come across in the future: If you store pointers in a vector the vector will destroy the pointer... but not what it is pointing at. For example, if we modify your program to use pointers to int instead of int directly:
int main()
{
std::vector<int *> tempVec;
int * a = new int;
int * b = new int;
*a = 55;
*b = 12;
tempVec.push_back(a);
tempVec.push_back(b);

//Do i need to call .clear at my tempVec here?
tempVec.clear()
//Or is it done automaticly at end of program?
return 0;
}


In this case the code is leaking memory because delete was not used on a and b. If you haven't really gotten to pointers this won't be too helpful, but it seems to be a common pitfall for beginners when they start to learn memory management.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!


A stack allocated vector will automatically free its contents when it goes out of scope. In this case, you don't need to call clear().

Actually, the vector will automatically free its contents whenever if goes out of scope, regardless of being stack allocated or not.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

Actually, the vector will automatically free its contents whenever if goes out of scope, regardless of being stack allocated or not.


Untrue, a non-stack-allocated vector (one which uses a "new" keyword) will remain in memory after you exit the function, unless you call delete. Calling clear() is not enough in this case, as the vector will shrink, but will still leak some memory. But this is not the case in the original code, since there the vector is stack allocated.

stack allocated:
std::vector<int> tempVec;

non-stack allocated:
void foo(){
std::vector<int> *tempVec=new std::vector<int>();
...
delete tempVec; //correct way to release memory
//temvec->clear() is not enough and not necessary. It will only clear the vector contents, and will leave some garbage in memory
}

My Oculus Rift Game: RaiderV

My Android VR games: Time-Rider& Dozer Driver

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown & Killer Bees


[quote name='larspensjo' timestamp='1345234092' post='4970655']
Actually, the vector will automatically free its contents whenever if goes out of scope, regardless of being stack allocated or not.


Untrue, a non-stack-allocated vector (one which uses a "new" keyword) will remain in memory
[/quote]

You are right, of course. I was thinking of global parameters. They are not allocated on the stack, but will still be deallocated automatically. That is why the original statement is correct but not complete. It is a little tricky to refer to allocation on the "stack", which I think is an implementation detail and not part of the language specification. The word "stack" is not part of the http://www-d0.fnal.gov/~dladams/cxx_standard.pdf (older version). I may be wrong here, but it is still precarious to refer to "stack".

I am not sure about the exact definition of "out of scope", but it could be argued that data allocated by "new" does not go out of scope until you "delete" it. The pointer can go out of scope automatically, but not the content.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
If you want precise definitions, a "stack allocated" variable is short hand for a variable with "local scope and automatic storage duration", which includes variables referred to with function parameter names. In C++ all names have scope, which is the declarative region in which the name is valid. When those names refer to variables, the objects referred to by those variable names are said to have the same scope. For local variables, when program flow passes a variable's declaration, that variable is said to be "in scope". When program flow leaves the block that the variable was declared in, that variable is said to move "out of scope". When an object with local scope and automatic storage duration moves out of scope, its destructor is called. However, variables with local scope can also have static storage duration. When program flow leaves the block that those variables are declared in, those objects are not automatically destroyed. Objects and variables not defined in a local scope cannot be meaningfully said to move in or out of scope. Such other objects are still constructed and destroyed, but the circumstances that those events occur have different terms associated with them.
I think you guys are going over the top with the answer, unless I am mistaken the basic question was wether or not a vector needs to be cleared before exiting the application it was used in, not really a question about scope.

I am curious as well about that, I often wonder if my code is leaving junk in the systems memory after a coding session... initially I assumed when the application ended all its resources was freed, but when I starting getting into SDL the tutorials I was following insisted in freeing the surfaces and quiting SDL before the application finished.

This topic is closed to new replies.

Advertisement