How do I create an invalid pointer for debugging?

Started by
7 comments, last by gimp 21 years, 9 months ago
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
Chris Brodie
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
//In the translation unit/.cpp file
CCommand* CCommand::INVALID = 0xFFFFFFFF;
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thanks guys.

Chris Brodie
http:\\fourth.flipcode.com
Chris Brodie
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
Chris Brodie
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
Try (CForm *) -1. Are you using .NET by any chance?
---visit #directxdev on afternet <- not just for directx, despite the name
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).

*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]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement