Advertisement

My First Class!!

Started by August 09, 2002 02:28 PM
12 comments, last by algumacoisaqualquer 22 years, 4 months ago
quote: Original post by algumacoisaqualquer
Thunder_Hawk - Well... Thanks!

dead_man_walking - sorry, but that didn''t made it, is getting the same error. I didn''t get the error with if (!ScreenMap){delete []ScreenMap;}, but I thinks this isn''t releasing any memory, so it is kind of pointless.

AP - I think that the problem is in the constructor, because it closed fine when I placed the delete part on coments, it has to be something there. i can post the code here, but i think it will be a little dificult to follow it, plus, I wasn''t geting any errors before using this class.

Thanks all you guys for the help!


First, please say that you are not using CreateMap anywhere outside of your constructors (its possible to fix it for that). Second, make sure you are not "delete"ing your pointers outside of your deconstructor (which I''m beginning to suspect). Third, I know its kind of nit-picky, but make sure you use delete like this:

"delete [] newed_array_pointer_name" or
"delete newed_pointer_name"

(notice the spacing around "[]")

Fourth, get used to this kind of problem... It happens quite often.

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Well, it is working now, but about the CreateMap, it is only called by the constructors, so it is only called once. The delete part is never called, but it is the destructor so it is outomatic, and the arrays are only deleted there. About the delete part, Now I''m using if (ScreenMap){delete [] ScreenMap;}, but even if i made some changes, there was no error.

Well, sorry guys, but I should have left the computer about half-hour ago, so I''ll have to leave now.

Again, thanks for all the help.
Advertisement
What you people seem to be missing is that you can delete null pointers. Try doing this, it won''t crash:


  int main(){    int *ptr = 0;    delete ptr;    return 0;}  


That is perfectly legal.


Death of one is a tragedy, death of a million is just a statistic.
If at first you don't succeed, redefine success.
quote: Original post by dead_man_walking
and destructors are called automatically where you program ends, unless the class in declared with new.)


No, the destructor is called when they go out of scope.

And to back up python regious, deleting null pointers is valid C++ so there''s no real point in:

if (pointer) delete pointer;

delete will check if it''s null anyway, and if it''s not null that''s no guarantee that it''s safe to delete.

This topic is closed to new replies.

Advertisement