Using "assert" with pointers in C ++

When we need to use "assert" for C ++ pointers, and when they are used, how are they most often implemented?

+4
source share
6 answers

Typically, you should use assert to check for a condition that, if false, indicates an error in your application. Therefore, if the NULL pointer should never occur at some point in the application, if there is no error, then approve it. If this can happen due to incorrect input, you need to perform the correct error handling.

+12
source

You don't need to use assert on pointers at all. The idea is to ensure that you don't work when dereferencing pointers when they are null.

You can do this with assert , but this is not a very professional way of handling such errors, since it invariably terminates the program - it is not a good idea if the user has not saved, for example, his last three hours of data entry.

What you have to do with pointers is check them for errors and grace. In other words, your function returns some kind of error or does nothing (not everyone will agree with this approach, but it is quite acceptable if it is documented).

The assert material is, in my opinion, meant to detect problems during development, so you will find that assert does nothing to build releases under some compilers. This does not replace defensive programming.

How to do it:

 #include <assert.h> void doSomethingWithPointer (int *p) { assert (p != 0); cout << *p << endl; } 

but it’s better to do it like:

 void doSomethingWithPointer (int *p) { if (p != 0) cout << *p << endl; } 

In other words, even if your “contract” (API) states that you are not allowed to get pointers to zero, you should still handle them gracefully. Old quote: be conservative in what you give, liberal in what you accept (rephrase).

+4
source

ASSERT statements are great, like "forced documentation", that is, they tell the reader something about the code ("This should never happen"), and then forcibly enters it, telling you if they are not true.

If this can happen (wrong input, memory cannot be allocated), it’s not the time to use ASSERT. Statements are intended only for things that cannot happen if everyone is subject to preconditions and the like.

You can do it as follows:

 ASSERT(pMyPointer); 
+4
source

From experience, if you say that under zero conditions it never happens under normal conditions, the program you are in very poor condition. Recovering from such a null condition is more likely to hide the original problem.

If you didn’t specify, in view of the guarantee of exclusion ( linky ), I say let it crash, then you know that you have a problem.

+2
source

I would use ASSERT, where a null pointer would not immediately cause a failure, but subsequently can lead to some kind of incorrect error, which is difficult to detect. eg:

 ASSERT(p); strcpy(p, "hello"); 

A little unnecessary, it just replaces the fatal exception with the fatal statement!
But in more complex code, in particular, such as smart pointers, it may be useful to find out if the pointer is what you are.

Remember that ASSERTs run only in debug builds, they disappear in release.

0
source

C also claims a function. in debug mode, if assert (x), condition x is false, a warning will appear ... But remember that it only works in debug mode ... in release mode all assert functions are all omitted

0
source

All Articles