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

L

lovecreatesbeauty

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

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

Bo Persson

lovecreatesbeauty said:
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
 
L

lovecreatesbeauty

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 )
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top