Jump to content
  • Advertisement

Archived

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

gimp

How do I create an invalid pointer for debugging?

This topic is 5983 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''ve gotten in to the habit of creating a static pointer in classes that are used as resources called INVALID. example:
  
class CCommand
{
   ...
   static CCommand* INVALID;
   ...
};
  
So when I''m returing say a failed creation of a command I return CCommand::INVALID and the calling program can test for that. When I was a bit more immature I had thought that just returning zero was good enough, however i''ve found this to be much more intuitive, readable and you can validly return a null pointer from a function if that was what it was meant to do and it won''t be confusing. Current however I generally do set the static definition to 0, simply because I cannot work out how to assign anything else. I''d like to assign INT_MAX to it or some other likely candidate. Any ideas on how to do this, or how to do it better? Many thanks Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
Advertisement
Throw an exception. That way they have to check for it... or the program terminates.


  
class CCommand
{
public:
class Invalid;
CCommand()
{
if( failed ) throw Invalid;
}
};

...
try
{
CCommand command;
// do stuff

}
catch( CCommand::Invalid& e )
{
// oops, no command... deal with it

}


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
Whoops posted to soon:

CForm* CForm::INVALID = 0xFFFFFFFF;

c:\ZZZ\Visual Studio Projects\Game\Engine\Form.cpp(37) : error C2440: ''initializing'' : cannot convert from ''unsigned int'' to ''CForm *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast


so I did this:

CForm* CForm::INVALID = (CForm*)0xFFFFFFFF;

c:\ZZZ\Visual Studio Projects\Game\Engine\Form.cpp(37) : warning C4312: ''type cast'' : conversion from ''unsigned int'' to ''CForm *'' of greater size


Not quite as easy as I thought...

Chris Brodie
http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ideally you would use the in-built ''NULL'' symbol. NULL is just another way of saying ''0'', but is specifically meant for pointers to make explicit that you are not talking about the integer ''0''. The two are interchangeable. However, you want to beware of using any value except NULL or ''0'' because both of these values are garaunteed to crash the program if you accidentally try to dereference them (i.e. there is a bug in your checking code). Any other number has undefined behavior if dereferenced improperly which tends to introduce subtle and hard to track bugs.

-D

Share this post


Link to post
Share on other sites
As the AP said, you should use NULL or 0. However, if you want to easily get the ''max int'' value I would not use 0xFFFFFFFF since that''s merely the maximum for 32 bit integers. I normally use ~0U (the bitwise inverse of unsigned zero).

Share this post


Link to post
Share on other sites
*sigh*

As I said before : use exceptions, that's what they've been designed for...

Alternatively, return the address of a static variable, instead of just a pointer with an arbitrary value :

class Foo
{
private:
static int INVALID_VAR;
public:
static int* INVALID;
};

int Foo::INVALID_VAR = 0;
int* Foo::INVALID = &Foo::INVALID_VAR;


Variable adresses are guaranteed to all be different.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on June 29, 2002 4:59:19 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!