views:

56

answers:

1

I spot some interesting articles about exception handle in CodeProject

http://www.codeproject.com/KB/cpp/seexception.aspx

After reading, I decided to do some experiment.

The first time I try to execute the following code

char *p;
p[0] = 0;

The program died without question.

But

After several times when I executed the same problem binary code, it magically did fine.

Even the following code is doing well. Any clue or explanation?

char *p;
p[1000] = 'd';
cout<<p[1000]<<endl;

My O/S is Windows 7 64bit and compiler is VS2008 rc1.

+1  A: 

Dereferencing a pointer that doesn't point to an object (for example, an uninitialized pointer) results in undefined behavior.

This means that anything can happen. Typically, writing via an uninitialized pointer will cause your program to crash--either immediately or at some point in the future. It is conceivable that your program could appear to continue running correctly, but you can never rely on that.

James McNellis
In your case, for example 1, you should probably set p to "0" if you want it to crash all the time.
Larry Osterman