Archived

This topic is now archived and is closed to further replies.

Pointer me to Confusion in C++

This topic is 5018 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been looking at Jim Adams's code (PROG ROLE PLAYING GAMES WITH DIRECTX) and are confused on how to deal with pointers. when should I be setting a pointer to NULL? Should I be setting any wanted pointers to NULL in the constructor whether it's pointing to directx or my own class or when finished set them back to NULL in the destructor? Should I not be doing it at all as they set/free them selves? Are there only certain times when you must set it to NULL or do you only do this when using new or delete? Even me writing this sounds confusing :-( Thanks in Advance! [edited by - EvilKind on March 19, 2004 7:58:52 AM]

Share this post


Link to post
Share on other sites
i usually set a pointer to null when its created, so if its in a class i set it to null in the constructor.
This allows you to call delete on the pointer in the destructor without worrying about whether or not its been initialized elsewhere in the class.

Share this post


Link to post
Share on other sites
Intention of setting pointer to point NULL address is that it won´t point to some place where it shouldn´t. So put pointer to NULL always when there is a chance that you will use it incorrectly, after delete, when initializing pointer (if you are not giving any other address)..

"Should I be setting any wanted pointers to NULL in the constructor whether it's pointing to directx or my own class or when finished set them back to NULL in the deconstructor?"

If you have pointers in your class, it´s not that important to put them NULL in destructor because they will be deleted and you can´t use them anymore..

So, always when pointer is in a state that it could be used to point some erroneus address then set it NULL so you can be sure that it won´t make any harms.


[edited by - Mkk on March 19, 2004 8:19:45 AM]

Share this post


Link to post
Share on other sites
Setting pointers to NULL before they are initialized, and after you are finished with them is a good way to make sure that you never use a pointer that points to nothing (since you can check if it''s NULL). This is not always necessary however, as it is often the case that while the pointer is in scope... it points to an object, which is either destroyed, or left alone as it falls out of scope... ie in many circumstances you should never be able to get a NULL pointer. It can however be the case in some circumstances that you need to allocate storage for pointers before the actual objects themselves... and having them be NULL before/after use can be a convenient way to indicate that something has occured rather than using other mechanisms. I do however think that some tutorials overstress the importants of this whole NULL usage, whereas I''d feel it more important to stress that it is preferable to only have pointers in scope that point to objects... and never bare pointers (unless absolutely necessary) but rather automatic pointers with share semantics (eg those provided within boost).

Share this post


Link to post
Share on other sites