• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# std::vector Question

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1BaneTrapper  Members

Posted 17 August 2012 - 08:46 AM

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;
}


### #2Brother Bob  Moderators

Posted 17 August 2012 - 08:48 AM

The content of the vector is automatically released when the vector is destroyed.

### #3SiCrane  Moderators

Posted 17 August 2012 - 08:49 AM

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().

### #4BaneTrapper  Members

Posted 17 August 2012 - 09:11 AM

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.

### #5nobodynews  Members

Posted 17 August 2012 - 11:25 AM

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!

### #6larspensjo  Members

Posted 17 August 2012 - 02:08 PM

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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #7SillyCow  Members

Posted 17 August 2012 - 03:26 PM

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 browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown &  Killer Bees

### #8larspensjo  Members

Posted 17 August 2012 - 06:26 PM

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

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.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #9SiCrane  Moderators

Posted 18 August 2012 - 09:28 AM

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.

### #10ASnogarD  Members

Posted 19 August 2012 - 05:43 AM

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.

### #11larspensjo  Members

Posted 19 August 2012 - 05:52 AM

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.

Unfortunately, this is C++ with a lot of history and legacy. So there are few simple answers.

However, when you exit your application, all resources are always automatically released (except external things like files). The ultimate "out of scope", as a matter of speaking. So why then go to the effort of freeing it yourself, if you are going to exit the application?

It is a good practice. You never know, when you start the project, what classes are going to have repeated allocate and deallocate patterns.
Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

### #12Brother Bob  Moderators

Posted 19 August 2012 - 06:24 AM

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.

The vector is automatically released because it goes out of scope. It doesn't matter that it happens to be the end of the main function. We mention scope because it is exactly that what is causing it to be automatically released.

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.

So basically your question is: is it all right to write sloppy and bad code that doesn't automatically manage object lifetimes and leak resources just because you can? No, it is not all right to do that in my opinion. Releasing resources at the end of the program is no different that releasing resources at any other point in your program when the resources aren't needed anymore. If you have properly designed resource handlers, then exiting the program is no different than any other situation where a resource shall be released.

Would you ask whether you need to release the resources when you, say, leave a level the user has just played in your game? I assure you, you would not leave those resources hanging around unreferenced within your program. The same solution that automatically handles the release of those resources should handle your program exit as well.

### #13SiCrane  Moderators

Posted 19 August 2012 - 08:35 AM

C++ offers one set of guarantees about cleanup when a process terminates normally and the client operating system may or may not offer another set of guarantees on top of that. On modern operating systems, the OS will do its best to clean up any resources it knows about; however, there are some resources that, either by accident or design, the OS will not free. For example, on Windows, ATOMs created by the GlobalAddAtom() function are explicitly noted to not be freed by application exit. Another fun one is that handles to a Windows semaphore are closed automatically by application exit; however, semaphore count is not affected by application exit, so other processes that are waiting on the semaphore to be signaled may wait forever on it.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.