Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > CHello *p = 0; p->f(); /* what does spec say about this*/

Reply
Thread Tools

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

 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      10-20-2011
class CHello{
public:
void f(){};
};

int main()
{
CHello *p = 0;
p->f(); /* undefined behavior? what does spec say about this*/
return 0;
}
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      10-20-2011
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


 
Reply With Quote
 
 
 
 
lovecreatesbeauty
Guest
Posts: n/a
 
      10-20-2011
On Oct 20, 11:45*pm, "Bo Persson" <(E-Mail Removed)> 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 )
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-20-2011
On 10/20/2011 1:55 PM, lovecreatesbeauty wrote:
> On Oct 20, 11:45 pm, "Bo Persson"<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to control order of spec execution in "spec specs/* " ? Andrew Chen Ruby 1 03-25-2008 12:36 PM
You say SIM, I say SEM Anon Computer Security 1 03-18-2006 01:49 PM
yaman.mustafa@chello.at yaman.mustafa Computer Support 2 01-16-2006 12:03 PM
pop.chello.at roswitha zauner Computer Support 1 09-12-2003 10:18 PM
hilfe-e-mail-server/ barbara.konic@chello.at barbara konic Computer Support 4 08-25-2003 06:08 PM



Advertisments