Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11011 > unrolled thread
| Started by | lovecreatesbeauty <lovecreatesbeauty@gmail.com> |
|---|---|
| First post | 2011-10-20 07:24 -0700 |
| Last post | 2011-10-20 13:19 -0500 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.c++
CHello *p = 0; p->f(); /* what does spec say about this*/ lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2011-10-20 07:24 -0700
Re: CHello *p = 0; p->f(); /* what does spec say about this*/ Leigh Johnston <leigh@i42.co.uk> - 2011-10-20 16:12 +0100
Re: CHello *p = 0; p->f(); /* what does spec say about this*/ "Bo Persson" <bop@gmb.dk> - 2011-10-20 17:45 +0200
Re: CHello *p = 0; p->f(); /* what does spec say about this*/ lovecreatesbeauty <lovecreatesbeauty@gmail.com> - 2011-10-20 10:55 -0700
Re: CHello *p = 0; p->f(); /* what does spec say about this*/ Victor Bazarov <v.bazarov@comcast.invalid> - 2011-10-20 14:09 -0400
Re: CHello *p = 0; p->f(); /* what does spec say about this*/ Robert Wessel <robertwessel2@yahoo.com> - 2011-10-20 13:19 -0500
| From | lovecreatesbeauty <lovecreatesbeauty@gmail.com> |
|---|---|
| Date | 2011-10-20 07:24 -0700 |
| Subject | CHello *p = 0; p->f(); /* what does spec say about this*/ |
| Message-ID | <764b3f1e-9b4e-495d-81ef-ec5a65264c9c@r2g2000prh.googlegroups.com> |
class CHello{
public:
void f(){};
};
int main()
{
CHello *p = 0;
p->f(); /* undefined behavior? what does spec say about this*/
return 0;
}
[toc] | [next] | [standalone]
| From | Leigh Johnston <leigh@i42.co.uk> |
|---|---|
| Date | 2011-10-20 16:12 +0100 |
| Message-ID | <H-Wdnc3ey_X3pz3TnZ2dnUVZ7o2dnZ2d@giganews.com> |
| In reply to | #11011 |
On 20/10/2011 15:24, lovecreatesbeauty wrote:
> class CHello{
> public:
> void f(){};
> };
>
> int main()
> {
> CHello *p = 0;
> p->f(); /* undefined behavior? what does spec say about this*/
> return 0;
> }
Yes it is undefined behaviour; you should not do it unless your name is
Microsoft. :)
/Leigh
[toc] | [prev] | [next] | [standalone]
| From | "Bo Persson" <bop@gmb.dk> |
|---|---|
| Date | 2011-10-20 17:45 +0200 |
| Message-ID | <9gau0cFseoU1@mid.individual.net> |
| In reply to | #11011 |
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
[toc] | [prev] | [next] | [standalone]
| From | lovecreatesbeauty <lovecreatesbeauty@gmail.com> |
|---|---|
| Date | 2011-10-20 10:55 -0700 |
| Message-ID | <f5eefa0b-3575-4c7c-8e54-265155222b6f@c25g2000yqn.googlegroups.com> |
| In reply to | #11020 |
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 )
[toc] | [prev] | [next] | [standalone]
| From | Victor Bazarov <v.bazarov@comcast.invalid> |
|---|---|
| Date | 2011-10-20 14:09 -0400 |
| Message-ID | <j7po5k$a0m$1@dont-email.me> |
| In reply to | #11025 |
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
[toc] | [prev] | [next] | [standalone]
| From | Robert Wessel <robertwessel2@yahoo.com> |
|---|---|
| Date | 2011-10-20 13:19 -0500 |
| Message-ID | <3vo0a759345bt9dhlflqcq2fsifguac18d@4ax.com> |
| In reply to | #11025 |
On Thu, 20 Oct 2011 10:55:49 -0700 (PDT), lovecreatesbeauty
<lovecreatesbeauty@gmail.com> 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 )
That's internal compiler magic for implementing offsetof. It actually
invokes three* different kinds of undefined (by the standard) behavior
to produce a constant expression for the value desired. With
sufficient knowledge of the compiler's internals, the *implementation*
can get away with that sort of thing.
Note that if the three undefined things are not supported by the
compiler in the required way, the compiler is still obligated to
provide offsetof, but will need to use different internal compiler
magic to do so. The above trick is popular because many compilers,
particularly those supporting flat address spaces, actually Do The
Right Thing with that expression, making the implementation of
offsetof trivial.
Note that unlike your example, a compiler that supports those tricks
for implementing offsetof does not actually dereference the NULL
pointer.
*It casts an integer zero to an object pointer, then performs
arithmetic on the result, and allows that in a constant expression.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web