Advertisement

How to remove a class from memory

Started by June 14, 2000 12:56 PM
7 comments, last by Possibility 24 years, 6 months ago
I was just wondering, in my game I have some classes, structs, and functions that get used in the begining durning the game setup, and after that they are never used again. So can these class then be removed from the system memory or anything like that, is that all done automatically by the computer? Possibility
Dynamically create you classes and structures with the keyword "new" and remove them when your done with "delete."

Object NewThing = new Object;
//stuff done here
delete NewThing;

Otherwise your objects'' destructors will never be called (at least not until main() ends).
Advertisement
Mmm, actually it would be:

Object* NewThing = new Object;
//stuff done here
delete NewThing;

new returns a pointer to the newly allocated class.
Good luck!
So is this what I do?

        class MYCLASS{//what ever in here};class MYCLASS MyClass;//but instead of that I should do:Object* MyClass = new MYCLASS;        



but that doesnt make any sense, so how do I actually do it?

Possibility

Edited by - Possibility on June 14, 2000 3:21:02 PM
Well, to be precise: You can''t create or delete a class - you define them. Only the object you create from it can be deleted.

    class MYCLASS{  //what ever in here};void Function(){  MYCLASS *MyObject;  MyObject = new MYCLASS;  // use it  delete MyObject;}    


Cool, I understand that now, i will have to try it out when I get home from work.
Thanks


Oh, one last thing, is this actually worthwhile to do? Does make any difference in game performance?

Possibility


Edited by - Possibility on June 14, 2000 4:20:49 PM
Advertisement
What does object* do? It kinda looks like a pointer.
If your program doesn''t use it anymore after the very beginning then it is good idea to use it in that way.
Especailly if you have a program that uses lots of memory.
Any way you can conserve memory is good.


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
themGames Productions

If these are global objects that get used once and never again, why not move them to the scope where they are used, making them local? Making them local obviously means they get deallocated automatically when you leave that scope.

If, for some reason, you need globals for this (say, you use them in 2 functions before abandoning them) then, again, it is safe as long as they are allocated on the stack as opposed to on the heap. (ie. you say ''ObjectName object'' rather than ''ObjectName* objectPtr = new ObjectName''). This ensures they get deleted ok, but they stick around for your game''s duration. Not what you want, I assume. Read on...

If you are allocating them with new, you need to delete them in your shutdown/cleanup function which you call when the game finishes. You can also delete them explicitly as soon as you''re done with them at any other point.

If you are referring to class/function -definitions- as opposed to class/function -instances-, then no, you can''t ''remove them from memory''. Your functions are a block of code in the executable and you can''t go changing the executable while it''s running. (Ignoring self-modifying code, but that''s not really relevant here.) And I don''t actually think a class definition takes up any space at run-time, but I am not familiar with how C++ handles this side of things. Either way, it is not a significant loss compared to a couple of extra graphics, or a few thousand empty UNIT structs

This topic is closed to new replies.

Advertisement