CHello *p = 0; p->f(); /* what does spec say about this*/

Discussion in 'C++' started by lovecreatesbeauty, Oct 20, 2011.

  1. class CHello{
    public:
    void f(){};
    };

    int main()
    {
    CHello *p = 0;
    p->f(); /* undefined behavior? what does spec say about this*/
    return 0;
    }
    lovecreatesbeauty, Oct 20, 2011
    #1
    1. Advertising

  2. lovecreatesbeauty

    Bo Persson Guest

    lovecreatesbeauty wrote:
    > class CHello{
    > public:
    > void f(){};
    > };
    >
    > int main()
    > {
    > CHello *p = 0;
    > p->f(); /* undefined behavior? what does spec say about this*/
    > return 0;
    > }


    You are not allowed to dereference a null pointer so, yes, this is
    undefined.


    Bo Persson
    Bo Persson, Oct 20, 2011
    #2
    1. Advertising

  3. On Oct 20, 11:45 pm, "Bo Persson" <> wrote:
    > lovecreatesbeauty wrote:
    > > class CHello{
    > > public:
    > > void f(){};
    > > };

    >
    > > int main()
    > > {
    > > CHello *p = 0;
    > > p->f(); /* undefined behavior? what does spec say about this*/
    > > return 0;
    > > }

    >
    > You are not allowed to dereference a null pointer so, yes, this is
    > undefined.


    Thanks, But the book `C: A reference manual, 5th' also mentions that
    stddef.h may define offsetof like this:

    #define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
    lovecreatesbeauty, Oct 20, 2011
    #3
  4. On 10/20/2011 1:55 PM, lovecreatesbeauty wrote:
    > On Oct 20, 11:45 pm, "Bo Persson"<> wrote:
    >> lovecreatesbeauty wrote:
    >>> class CHello{
    >>> public:
    >>> void f(){};
    >>> };

    >>
    >>> int main()
    >>> {
    >>> CHello *p = 0;
    >>> p->f(); /* undefined behavior? what does spec say about this*/
    >>> return 0;
    >>> }

    >>
    >> You are not allowed to dereference a null pointer so, yes, this is
    >> undefined.

    >
    > Thanks, But the book `C: A reference manual, 5th' also mentions that
    > stddef.h may define offsetof like this:
    >
    > #define offsetof(type, member) ( (size_t)& ((type*)0) -> member )


    There is no provision in the Standard regarding the implementation of
    the C language 'offsetof' macro *except* that its 'type' argument is
    limited to the POD types. That means that any other requirement of the
    Standard shall be followed when implementing that macro, which means
    that if it is implemented to dereference a null pointer, it's invalid.

    As for the book, how is a book about implementation of some C language
    construct be relevant here? This is a C++ newsgroup.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 20, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek Fountain
    Replies:
    1
    Views:
    389
    Martin Honnen
    Jul 2, 2004
  2. Replies:
    2
    Views:
    1,923
    Andrey Tarasevich
    Nov 26, 2008
  3. edam
    Replies:
    14
    Views:
    1,293
    Paul Bibbings
    May 10, 2010
  4. Andrew Chen
    Replies:
    1
    Views:
    189
    David Chelimsky
    Mar 25, 2008
  5. Ben Bacarisse

    Re: why does thie variable length size spec compiles

    Ben Bacarisse, Sep 6, 2013, in forum: C Programming
    Replies:
    0
    Views:
    182
    Ben Bacarisse
    Sep 6, 2013
Loading...

Share This Page