   releasing memory
  hey everybody....this is my very first post!!i was wondering if anyone could help with my problem. Basically i have got a player class and a lazer class (ie weapon). Lazer objects are instantiated in the fireWeapon function in the player class like so:CLazer *newLazer = new CLazer(lazerPositionX, lazerPositionY, lazerPositionZ);and i want to delete that instance of the lazer when it has travelled a certain amount of world units. Ive got a getExplosion() function returning a boolean in CLazer when it travels a number certain amount of units but i dont know how to actually delete the instance of it in CPlayer or whereabouts to do it. Can anybody help?? 
  Try this:delete newLazer; //deletes it from the heapnewLazer = NULL; //means the pointer can''t do any damage.I think that''s right. I shall be ridiculed if it''s a lie! ;  Doing this is a very bad idea because you're causing memory leaks.  If you don't keep track of the pointer you get from the memory allocation you'll just lose it once the function returns and there is no easy way to find out where it is and how to get rid of it. You can delete it at the end of the function and not have this problem, but then you won't be able to do anything with the lazer outside of "fireWeapon." In you case you want to have a CLazer as a member of the player class, and work with that. So...class CPlayer{public:void fireWeapon();int getExplosion();private:CLazer* lazer;};   If the player can only fire a single lazer it's a better idea to just have a single CLazer variable and not have to worry about memory allocation, i.e. class CPlayer{public:void fireWeapon();int getExplosion();private:CLazer lazer;};   You can keep track of the lazer's state with a variable inside of the lazer, rather then it's whether it exists in memory. So your lazer class might look like this (everything is public to make the code simpler):#define LAZER_INACTIVE 0#define LAZER_FIRE 1#define LAZER_EXPLODE 2class CLazer{public:float xPosition, yPosition, zPosition;int state;};   You could can then read the state of the lazer and decide whether it should be not active, moved and collision tested or exploding.  For small objects like lazers (and any game object in general) it's better to have a pool of memory that can keep hold a lot of lazers and keep track of which lazers are currently active.  It's not a good idea to use new and delete for every time you need a new lazer and every time a lazer is destroyed.  This has to do with the fact that allocating memory in a modern OS (Windows, Linux) is generally slow due to the complexity of the memory managers.  Without going into writing your own more general purpose memory manager, it would be best to either just have a static array of X amount of lazers (an extension of the last suggestion):class CPlayer{public:void fireWeapon();int getExplosion();private:CLazer lazer[MAX_LAZERS];};   Or a dynamic array of lazers that is initialized with the player (a good idea if you have a lot of lazers.)class CPlayer{public:void fireWeapon();int getExplosion();void initLazers(int numLazers);private:CLazer* lazer;int m_numLazers;};int CPlayer::initLazers(int numLazers){    lazer = new CLazer[numLazers];    m_numLazers = numLazers;}   Or perhaps an STL vector or list of lazers.  It may also be a good idea to have a global lazer pool and keep track of who a lazer belongs to in the lazer class (player, enemy, friend, etc.) My suggestions aren't necessarily the"best" way to do handle this, but they work well for a small game and have pretty straightforward implementation. Also, keep in mind all of my classes are there to better demonstrate my suggestions, they are obviously missing a lot of stuff that you would have in your actual game.   [edited by - impossible on February 10, 2004 11:35:05 AM];  cheers for your help. I have tried declaring CLazer *lazer in the header, but have been getting the error g:\gaming\Zaxxon\player.h(35): error C2143: syntax error : missing '';'' before ''*''and i cant for the life of me find out why. i dont suppose you have any ideas on that one do you?;  ... you''re probably not including your declaration of CLazer before the CLazer *lazer, so the compiler doesn''t know what you''re talking about. You can help Mr Compiler out of this pickle by:#include "Whatever File CLazer is declared in.h"... before you declare your CPlayer class.or, preferably, by using a forward declaration (avoids the include and since you are only talking about a pointer here, you only need to say it exists, not the details of it):// Forward declaration:class CLazer;//class CPlayer  {// [etc.etc.etc.]  CLazer *lazer;  };On the other hand, I could be outstandingly wrong. But without your source code, who knows ;  it aint the lack of a header im afraid, but cheers for the hint about forward declarations...ive seen the syntax before and wondered what it was for!! ;  found out the problem just in case anybody gets the same problem, was because i was #including player in CLazer as well as #including lazer into CPlayer. Turned out CLazer didnt need to know about CPlayer anyway so i could just delete it!! 