liveBook · Manning

Invalid pointer

As explained in Modern C, Third Edition

An invalid pointer is a pointer value whose state does not permit reliable evaluation or use according to the C language rules.

An invalid pointer has an indeterminate state that cannot be safely evaluated. Evaluating it produces an invalid value and places the program in an undefined state. section 6.2

An uninitialized pointer

char const*const p2invalid;\n\nBecause p2invalid is uninitialized, its state is indeterminate. Reading or otherwise evaluating it can fail; initialize a pointer or set it to null when validity cannot be guaranteed. section 6.2

Keep these pointer-safety rules in view:

  • Do not confuse invalid and null pointers: logical tests can identify null pointers but cannot reliably distinguish valid pointers from invalid ones. section 6.2
  • Do not dereference an invalid pointer. Using * with one can fail, potentially accessing an unintended memory object and causing difficult-to-trace bugs. section 11.1.1
char const*const p2invalid;

Inspect the uninitialized declaration that demonstrates how an indeterminate pointer state arises. section 6.2

How Modern C, Third Edition covers it

How deep it goes
Traces invalidity from uninitialized state through evaluation, undefined behavior, and dereference failure.
Examples
Uses an uninitialized pointer declaration and contrasts invalid-pointer dereferencing with null-pointer behavior.
What it assumes
Assumes pointer types and expressions, while distinguishing valid, null, and invalid pointer states.
What you can do after
Apply it by initializing pointers and avoiding dereference until a valid or deliberately null state is established.
How it connects
Connects invalid pointers with null pointers, pointer initialization, dereferencing, address-of operators, and undefined behavior.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.