8
#include <iostream>

int main()
{
    int* i = 0;
    int x = (*i);
    std::cout << x;
}

The above program will crash when I compile and run it using Visual Studio 2010 and I know it crashes because I set the pointer to 0.

What I would like to know, is accessing a null pointer in C++ defined in the standard or is it undefined and I just happen to get lucky that my program crashed because of my compiler/computer/operating system

If it is defined, what does C++ guarantee me when I try and access a null pointer?

Caesar
  • 9,483
  • 8
  • 40
  • 66
  • 1
    @R.MartinhoFernandes So I can expect that on some computers this will not crash? – Caesar Jun 12 '13 at 08:48
  • 1
    yes, but you can also expect it to format your harddrive on others – Serve Laurijssen Jun 12 '13 at 08:48
  • @Caesar: Correct. On primitive architectures without memory protection schemes, the null pointer usually refers to address 0, which is usually readable. – Marcelo Cantos Jun 12 '13 at 08:49
  • http://stackoverflow.com/a/2727872/986760 – fkl Jun 12 '13 at 08:49
  • @Caesar Consider the old DOS based systems, where the interrupt table starts at address 0, yet address 0 is also the address used for null pointers. Reading from null pointers would return garbage, writing to null pointers would blow up quite spectacularly in some unrelated later code that happens to use the interrupt whose handler was accidentally overwritten. –  Jun 12 '13 at 08:50
  • Thanks everyone, I didn't know there was already a question like this. I would delete this question but it already has an answer – Caesar Jun 12 '13 at 08:52
  • @hvd: The null pointer isn't necessarily 0. Though I don't think they ever did, DOS C compilers might have been able to set the null pointer to some kind of address that triggered an interrupt when accessed. (I don't know whether PC hardware supported such a technique, however). – Marcelo Cantos Jun 12 '13 at 08:53
  • @MarceloCantos I know the standard allows that, but like you say, compilers didn't do so. I think it would have been possible, not the entire address space was mapped in those days. –  Jun 12 '13 at 08:58
  • 6
    On the DeathStation 9000, which implements a perfectly standards-compliant C++ compiler, dereferencing a NULL pointer beams a kitten into space. – Kaz Dragon Jun 12 '13 at 09:12
  • 4
    @KazDragon although, sometimes it beams space into a kitten. – Peter Wood Jun 12 '13 at 09:20

3 Answers3

9

Dereferencing a null pointer will invoke undefined behavior. It may result in different things on different compilers, even more - different things may happen on the same compiler if compiled multiple times. There are no guarantees of the behavior at all.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
3

What makes your process crash here is the OS stopping your program from fiddling with memory it does not have access to (at address 0). Windows will give you an "Access violation", Linux/Unix will give you a "segmentation fault".

Also, see Why are NULL pointers defined differently in C and C++? for a quote of what a null pointer is in the standard

Community
  • 1
  • 1
rectummelancolique
  • 2,247
  • 17
  • 13
2

It is not defined in C++ so it may not crash on some operating systems, but you can count on a crash under current (and previous) versions of Windows and Linux because neither of those will let you (as a user process) access that memory location.

Also, under Windows, if you want to cause a program break, try DebugBreak(); which causes an exception (MSDN says: Causes a breakpoint exception to occur in the current process. This allows the calling thread to signal the debugger to handle the exception.)

Terminality
  • 799
  • 6
  • 11