• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BaneTrapper

std::vector Question

12 posts in this topic

Hello.
I am currently using vectors.
But i am unsure do i need to call myVector.clear before my application ends?
[CODE]
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;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1345214940' post='4970562']
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().
[/quote]
Thanks for quick replay and clear answer.
0

Share this post


Link to post
Share on other sites
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:
[CODE]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;
}[/CODE]

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

Share this post


Link to post
Share on other sites
[quote name='SiCrane' timestamp='1345214940' post='4970562']
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().
[/quote]
Actually, the vector will automatically free its contents whenever if goes out of scope, regardless of being stack allocated or not.
0

Share this post


Link to post
Share on other sites
[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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='SillyCow' timestamp='1345238767' post='4970677']
[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.
[/quote]

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 [url="http://www-d0.fnal.gov/~dladams/cxx_standard.pdf"]http://www-d0.fnal.gov/~dladams/cxx_standard.pdf[/url] (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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='ASnogarD' timestamp='1345376608' post='4971079']
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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote name='ASnogarD' timestamp='1345376608' post='4971079']
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.
[/quote]
The vector is automatically released [i]because[/i] 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.

[quote name='ASnogarD' timestamp='1345376608' post='4971079']
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.
[/quote]
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.
0

Share this post


Link to post
Share on other sites
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 [url=http://msdn.microsoft.com/en-us/library/ms649060%28v=VS.85%29.aspx]GlobalAddAtom()[/url] function are explicitly noted to not be freed by application exit. Another fun one is that handles to a Windows [url=http://msdn.microsoft.com/en-us/library/ms685129%28v=vs.85%29.aspx]semaphore[/url] 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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0