Friday, January 20, 2012

Dereferencing a NULL pointer

The following code does not cause a run-time error, even it is dereferencing a NULL pointer:

#include <stdio.h>

class C
{
public:
    int one()
    {
        printf("this=%08X\n", this);
        return 1;
    }
};

int main(int argc, char *argv[])
{
    printf("== Start\n\n");

    C* c = 0;

    printf("one=%d\n\n", c->one());

    printf("one=%d\n\n", (*c).one());

    printf("== End\n");
    return 0;
}

Output is:

== Start

this=00000000
one=1

this=00000000
one=1

== End
Press <RETURN> to close this window...

No comments:

Post a Comment