On 10/20/2011 1:55 PM, lovecreatesbeauty wrote:
> On Oct 20, 11:45 pm, "Bo Persson"<b...@gmb.dk> 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
|