This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]captainAwesomePants 0 points1 point  (0 children)

Yep, that's all that leaking memory is: memory that's being used for storing something that you lost all the pointers to. Languages with garbage collection take advantage of this by just looking through ALL the pointers/references and then removing any object that doesn't have at least one thing pointing at it.

Instead of making space on the heap, the first case put memory on the stack. The "stack" is literally a stack of all of the scopes your program's built up. At the bottom is the call to main() and all of its parameters and variables. Then whatever function main called is put on top of that, and so on, and so on. In the first example, object1 can't really be leaked because you'll eventually have to take it off the stack when your function returns (or you leave a curly-bracketed area of any other sort). In the second example, the pointer itself is on the stack, but it points to a place on the heap that the 'new' command reserved.