MS Windows - C++ how to check if pointer is valid?

longjumps
longjumps used Ask the Experts™
on
How to check if pointer is valid in C++ code?

Let's say macros "IsValidPtr", how it should looks like?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
How do you define "valid" ?
It is possible only in specific cases, e.g. you have collection of objects and you can check whether the pointer points to some of them (in the sense IsValidWindow() or something like that).

Pointer is just 4 or 8 bytes that store a number that is interpreted as a memory address.  Any value is valid, in some sense.

A compiler or a programmer may use a special value to represent something "invalid" in the sense of uninitialized.  However, it is compiler/debugger/platform specific.

Author

Commented:
I need a code example for pointer validation. Not just that it is not NULL, but all other things.
Top Expert 2016

Commented:
you could have 3 kinds of invalid pointers. the first is a null pointer. the second is a pointer already deleted but used after deletion. a special form of that kind is if you return a pointer to a local object which was deleted (removed from memory) automatically after return. the third kind is a corrupted (or uninitialized) pointer what means that the address was not returned by 'new' operator and was not the address of an existing object (beside by accident).  

the null pointers could be checked easily though not all functions would do that. for example a strcpy normally crashes if one of the pointers is NULL.
you could avoid much of the pointers of the second kind if you consequently set pointer variables to NULL after deletion. that doesn't help if you have two pointer variables pointing to the same object, but that normally is bad programming. you also could avoid the second case by using only smart pointers which would care for deletion theirselves.

the third kind of invalid pointers isn't a case which should be handled but you need to find the cause for the corruption. memory corruption will harm not only pointers but could harm all of your code. you will not be able to continue successfully in most cases even if you would try to catch such violations.  so you should try to avoid such corruptions by consequently initializing all pointer variables and never return pointers to local variables or pass such pointers to asynchronously running threads.

in visual c++ if you compiled in debug mode there are some checks added to help against invalid pointers or to help to find wrong code in case of a crash. for example, if you delete an instance of a class that has member pointers, the debugger would set those member pointers to 0xcdcdcdcd (in 32-bit). if your program crashes later and shows such pointer contents, you would know that you used a pointer member of a deleted object.  

Sara
Top Expert 2009

Commented:
>> Not just that it is not NULL, but all other things.

What are "all other things" ?
Top Expert 2016

Commented:
in managed c++ you have a memory manager which would care for pointers.

in c++ you are lost beside you add your own pointer management (what isn't a realistic goal if you have to use libraries with pointers in the public interface).

however, you could try to program without pointers. that will not work without exceptions but if you made those exceptions safe you don't need pointer validation anymore.

Sara

Author

Commented:
I just want to check if pointer valid, let's say I can divide it by 4 and so on.
Need code example.
> I just want to check if pointer valid, let's say I can divide it by 4 and so on.
> Need code example.


It cannot be done.  Impossible.
Top Expert 2009

Commented:
>> let's say I can divide it by 4 and so on.

What do you mean by that ? You want to check if the pointer is aligned to a 4-byte boundary ? Why ? What if non-4-byte aligned pointers are valid on your platform ?

Could you explain your use case ? Why do you want to check if a pointer is "valid" ?
Top Expert 2016

Commented:
char s[] = "ABCD";
char * p = &s[1];

Open in new window


the pointer p would have odd address value.
Top Expert 2012
Commented:
Top Expert 2016

Commented:
if you follow the links given by jkr, you will see that the IsBadxxxPtr functions are called obsolete by ms. i used one of them in some older projects  and never got a positive return. probably they were better than doing nothing in case you know for which pointers you would call them.

the links are interesting cause they recommend  to use structured exception handling (SEH) while dereferencing the pointer. jkr surely will help you if you want to use SEH.

Sara

Author

Commented:
checking solution.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial