NULL pointer dereferencing?

A

Angel Tsankov

Does the standard define what happens when a NULL pointer is dereferenced? If so, where?
 
B

Ben Pope

Angel said:
Does the standard define what happens when a NULL pointer is
dereferenced? If so, where?

That's probably the classic case of undefined behaviour.

Ben Pope
 
R

Ron Natalie

Angel said:
Does the standard define what happens when a NULL pointer is
dereferenced? If so, where?

It is in the definition of undefined behavior as a matter of
fact.
 
K

Kaz Kylheku

Angel said:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?

In some cases the behavior is defined, namely where the expression is
not actually evaluated.

#include <cstdlib>

// ...

type *p = (type *) malloc(sizeof *p);

Here p is dereferenced, and doesn't even have a value yet, let alone a
null one. That's okay, because the sizeof operator only needs the
expression for its type, not its value. The expression is not
evaluated.

By the way, don't call it a NULL pointer. There are null pointers,
null characters, the ASCII character NUL and the NULL constant.
Typography matters!

:)

There are some shady uses of null pointers which are strictly speaking
undefined, but are quasi-defined in the sense of having quite broad
portability.

You may find a use of a null pointer in the definition of the
offsetof() macro from ANSI C.

This is from the comp.lang.c FAQ:

9.8: How can I determine the byte offset of a field
within a structure?

ANSI C defines the offsetof macro, which should be used if available;
see <stddef.h>. If you don't have it, a suggested implementation is


#define offsetof(type, mem) ((size_t) \
((char *)&((type *) 0)->mem - (char *)((type *) 0)))

This implementation is not 100% portable; some compilers may
legitimately refuse to accept it.

As you can see, there is a dereference of a null pointer in there,
namely the ((type *) 0)->mem! The value is not used: rather, the
address of that dereferenced value is taken right away. The address is
subtracted from the null pointer, to make the construct portable to
systems where a null pointer does not convert to an integer zero, and
the result of the subtraction is the offset of that member within the
struct.

Pointer arithmetic on null pointers is also undefined.
 
G

Greg

Angel said:
Does the standard define what happens when a NULL pointer is dereferenced? If so, where?

The Standard leaves the behavior undefined in this case.

However, nearly every implementation does define the behavior (which an
implementation is free to do) - usually as a memory access violation.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top