Archived

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

garbage collection and stuff...(c++)

This topic is 6020 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

Hi! I have two questions about some c++ stuff. 1. can i call a destructor explicity when i want an object to seize to exist?? 2. how much garbage collection is done in c++?? any atall? thanx --Spencer

Share this post


Link to post
Share on other sites
1. You __can__ call a destructor explicitly, but you shouldn't. If you have an object allocated on the heap, with new, you should use delete. If you explicitly call the destructor, the destructor will execute, but the memory associated with the object will not be freed. The only thing that will ever require an explicit destructor call is an obscure language feature called placement new(look it up, I'm sure someone can explain it better) where you actually decide where in memory the object should go.
2. C++ has no garbage collection as such. Everything allocated on the heap has to be explicitly freed by the programmer. Of course, anything on the stack will be cleaned up with the rest of the stackframe.

Edited by - Eraserhead on June 22, 2001 9:53:34 AM

Share this post


Link to post
Share on other sites
Do yourself a favor and go get "Effective C++" by Scott Meyers.

Or visit your local mega book store and just read the first tip (or two) about using New and Delete.

Share this post


Link to post
Share on other sites
Well, most modern os''s including windows clean up memory after your program exits, but it''s not exactly considered good programming practice.

Share this post


Link to post
Share on other sites
Windows NT doesn''t. I ran a "leaker" program in a loop in a batch file, never saw my physical commit drop after I hit ctrl-C.

What''s dangerous is acquiring memory repeatedly without releasing it. Eventually, your program will crash--whether or not the OS reclaims that memory afterward is moot from a quality standpoint

Share this post


Link to post
Share on other sites