On Sep 29, 11:38*am, SeanW <sean_in_rale...@yahoo.com> wrote:
> On Sep 29, 8:33*am, prashant <b.prash...@gmail.com> wrote:
>
> > As mentioned its an undefined behaviour, but i had expected it to
> > throw a nullpointer exception which would have led me to detect the
> > error. anyways somethings need to be checked before using it..
>
> Standard C++ does not have a "nullpointer exception". *That's
> Java or Microsoft's SEH C++ extension or something else.
To be crystal clear, let me add this. De-referencing a null pointer is
undefined behavior according to the standard. In practice, on some
systems, this may throw a Windows SEH exception (not a C++ exception),
and on other systems it will kill the program and produce a core dump.
Undefined behavior means that the implementation can do whatever it
wants, such as die, print an error the console, throw an exception,
send an email to your mother, or format your hard drive. (Note that
your hard drive will almost certainly not be formatted by a null
pointer de-reference. It's just the common [exaggerated] response,
just like "Hello world!" is generally your first program. However, a
mean implementation is still standard compliant if it does format your
hard drive on a null pointer de-reference.)
PS: never do something which is undefined. By the very definition of
undefined, the result is not predictable, so you should not do it.
However, just because it's undefined by the C++ standard does not mean
it's undefined by all standards. Ex: POSIX and reinterpret_cast'ing
the result of a void* to a function pointer for dlsym. Just don't
expect your program to work on all C++ implementations, as POSIX is
now defining some semantics of your program.
|