Wednesday, November 23, 2011

((X *) 0)+1)

what is this strange thing computing?

2 comments:

  1. Pointer arithmetic :), displacement of sizeof(X) still acts as pointer.

    (unsigned)((X *) 0)+1) // sizeof(X)

    class X
    {
    public:
    display() { cout << "hi" }

    private:
    int gender;
    };

    ((X *)(0))->display(); // won't touch X substrate (no runtime surprises)

    C++ is fun to play around :)

    ReplyDelete