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

Started by
3 comments, last by Spencer 22 years, 10 months ago
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
--Spencer"All in accordance with the prophecy..."
Advertisement
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
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.
Dustin
Well, most modern os''s including windows clean up memory after your program exits, but it''s not exactly considered good programming practice.
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

This topic is closed to new replies.

Advertisement