Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "assert" with pointers in C++

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

like image 380
ohyeah Avatar asked Dec 10 '09 00:12

ohyeah


People also ask

What does assert () do in C?

C library macro - assert() The C library macro void assert(int expression) allows diagnostic information to be written to the standard error file. In other words, it can be used to add diagnostics in your C program.

Should I use assert in C?

You should only use assert to check for situations that "can't happen", e.g. that violate the invariants or postconditions of an algorithm, but probably not for input validation (certainly not in libraries). When detecting invalid input from clients, be friendly and return an error code.

How do you write assert statement in C?

Assertions in C/C++ Following is the syntax for assertion. void assert( int expression ); If the expression evaluates to 0 (false), then the expression, sourcecode filename, and line number are sent to the standard error, and then abort() function is called. For example, consider the following program.

What is assert 0 C?

assert(0) or assert(false) is usually used to mark unreachable code, so that in debug mode a diagnostic message is emitted and the program is aborted when the supposedly unreachable is actually reached, which is a clear signal that the program isn't doing what we think it is.


2 Answers

Generally you would use an assert to check a condition that, if false, would indicate a bug in your application. So if a NULL pointer shouldn't ever be encountered at some point in the application, unless there's a bug, then assert it. If it might be encountered due to some invalid input then you need to do proper error handling.

like image 106
EMP Avatar answered Oct 03 '22 09:10

EMP


You don't need to use assert on pointers at all. The idea is to ensure you don't crash when dereferencing your pointers when they're null.

You can do this with assert but it's not a very professional way to handle errors like this since it invariably terminates the program - not a good idea if the user hasn't, for example, saved their last three hours worth of data entry.

What you should do with pointers is to check them for null-ness and fail gracefully. In other words, have your function return an error of some sort or do nothing (not everyone will agree with this approach but it's perfectly acceptable if it's documented).

The assert stuff is meant, in my opinion, for catching problems during development which is why you'll find assert does nothing in release builds under some compilers. It is not a substitute for defensive programming.

As to how to do it:

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

but this would be better done as:

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

In other words, even if your "contract" (API) states that you're not allowed to receive null pointers, you should still handle them gracefully. An old quote: be conservative in what you give, liberal in what you accept (paraphrased).

like image 37
paxdiablo Avatar answered Oct 03 '22 10:10

paxdiablo