Skip to main content
gamedev.net
🔒 Locked

C++ Memory

Started by Mr_Ridd Apr 1, 2004 at 6:32 AM 5 replies 300+ views
Original Post
Mr_Ridd
Mr_Ridd
Howzit Say I have a class ''Player'' and I make a ''Player'' object ''play'' Player play; How would a free it from memory when I''m finished using it? In Java I would say: play = null; Shot
The ability to succeed is the ability to adapt
Mr_Ridd
Mr_Ridd
Ok, yes, I know. It doesn''t literally free it from memory but the same principle applies.

The object is not in local scope. It''s global. So I want to free it up when the program exits, or will this be done automatically?
The ability to succeed is the ability to adapt
RowanPD
RowanPD
If it is a local variable in a function then the memory for it is "released" in the same way as any other local variable is. C++ is smart enough to call it's destructor before it does so.

If it's a global then the same happens then the program exits. This is your situation. You have nothing to fear as it isn't a pointer to a class.

FYI, if it's a member of a class then it's the same as the first paragraph except the the timing is when the class is released. Remember it's not when destructed.

It's the same as any other variable in respect to memory used because it's memory is statically "allocated" from the heap. That is, the programs' heap pointer is moved to give room for it. (I think that is the right terminology)

Garbage collection is only an issue for specifically allocated memory, eg. use of "malloc/calloc" and "new".

EDIT: reworded

R

--------------------------------------------------------------------------
There is no point in flaming if you've merely poured fuel on your own head

[edited by - RowanPD on April 1, 2004 7:55:44 AM]
R--------------------------------------------------------------------------There is no point in flaming if you've merely poured fuel on your own head
Shadowdancer
Shadowdancer
The destructor will be called when the program exits. If you want to see when, add some print debugging to the destructor. Anyway, this should not be a serious problem since the OS will clean up every allocation the program made as long as you didn''t deliberately hide it ;-)

If you want more control, create objects locally (it''s easier to see the scope then). For even more control, you can use the new operator which should make it more "Java-like":

Player *play = new Player; 


You''ll have to use the dereferencing operator to access attributes or methods and make sure you clean up properly with

delete play; 


when you''re done with the object since C++ does not have garbage collection.
rohde
rohde
Such a global object is in what is known as static storage class, meaning that it exists for the duraion of the execution of the program.

It will be deleted automatically.



There''s no place like 127.0.0.1
"We confess our little faults to persuade people that we have no large ones." -Francois de La Rochefoucauld (1613 - 1680). | My blog

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.