[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Null Pointer Indirection

LJ

6/5/2011 8:55:00 AM

I have a doubt that why does the below expression not lead to null
pointer indirection error:

&(*(int * )0);

It runs successfully and evaluates to zero.
23 Answers

Tobias Blass

6/5/2011 9:04:00 AM

0

On 2011-06-05, LJ <djtheshowstopper@gmail.com> wrote:
>) I have a doubt that why does the below expression not lead to null
> pointer indirection error:
>
> &(*(int * )0);
>
> It runs successfully and evaluates to zero.

Perhaps the compiler optimized it to (int*)0.

China Blue Veins

6/5/2011 9:12:00 AM

0

In article <31c3cdfb-6b88-4d38-a806-8f12442540ef@j13g2000pro.googlegroups.com>,
LJ <djtheshowstopper@gmail.com> wrote:

> I have a doubt that why does the below expression not lead to null
> pointer indirection error:
>
> &(*(int * )0);

0 NULL rvalue.
(int*)0 int* NULL rvalue.
*(int*)0 NULL address lvalue.
&*(int*)0 int* NULL rvalue.

*x is not a dereference, but a convert pointer to lvalue. In order to get an
error you need to load the lvalue into an rvalue. Instead you convert the lvalue
back to an rvalue.

--
I remember finding out about you, | I survived XYZZY-Day.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room |Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

LJ

6/5/2011 10:18:00 AM

0

On Jun 5, 2:04 pm, Tobias Blass <tobiasbl...@gmx.net> wrote:
> On 2011-06-05, LJ <djtheshowstop...@gmail.com> wrote:
>
> >) I have a doubt that why does the below expression  not lead to null
> > pointer indirection error:
>
> > &(*(int * )0);
>
> > It runs successfully and evaluates to zero.
>
> Perhaps the compiler optimized it to (int*)0.

So is it implementation-dependent to give an error or not??
An implementation of offsetof() macro uses a technique similar to this

pete

6/5/2011 11:28:00 AM

0

LJ wrote:
>
> On Jun 5, 2:04 pm, Tobias Blass <tobiasbl...@gmx.net> wrote:
> > On 2011-06-05, LJ <djtheshowstop...@gmail.com> wrote:
> >
> > >) I have a doubt that why does the below expression not lead to null
> > > pointer indirection error:
> >
> > > &(*(int * )0);
> >
> > > It runs successfully and evaluates to zero.
> >
> > Perhaps the compiler optimized it to (int*)0.
>
> So is it implementation-dependent to give an error or not??
> An implementation of offsetof() macro uses a technique similar to this

In C99, there's a special rule for & and * together.

6.5.3.2 Address and indirection operators
Semantics
3
The unary & operator returns the address of its operand.

If the operand is the result of a unary * operator,
neither that operator nor the & operator is evaluated
and the result is as if both were omitted,
except that the constraints on the operators
still apply and the result is not an lvalue.

--
pete

Eric Sosman

6/5/2011 11:29:00 AM

0

On 6/5/2011 6:17 AM, LJ wrote:
> On Jun 5, 2:04 pm, Tobias Blass<tobiasbl...@gmx.net> wrote:
>> On 2011-06-05, LJ<djtheshowstop...@gmail.com> wrote:
>>
>>> ) I have a doubt that why does the below expression not lead to null
>>> pointer indirection error:
>>
>>> &(*(int * )0);
>>
>>> It runs successfully and evaluates to zero.
>>
>> Perhaps the compiler optimized it to (int*)0.
>
> So is it implementation-dependent to give an error or not??

Not implementation-dependent. When the operand of the address-of
operator & is the result of applying the indirection operator *, the
two "cancel each other out" and neither is evaluated. (Section
6.5.3.2 paragraph 3 of the C Standard, if you'd like to look it up.)

> An implementation of offsetof() macro uses a technique similar to this

You haven't shown the implementation, but I suspect that if you
examine it closely you'll find there's a little more going on than
just "& swallows *."

--
Eric Sosman
esosman@ieee-dot-org.invalid

pete

6/5/2011 11:31:00 AM

0

China Blue Angels wrote:

> *(int*)0 NULL address lvalue.

No, that's just wrong.

6.5.3 Unary operators

footnote 83)
Among the invalid values for dereferencing a pointer
by the unary * operator
are a null pointer,

--
pete

China Blue Veins

6/5/2011 12:18:00 PM

0

In article <4DEB68F3.7550@mindspring.com>, pete <pfiland@mindspring.com> wrote:

> China Blue Angels wrote:
>
> > *(int*)0 NULL address lvalue.
>
> No, that's just wrong.
>
> 6.5.3 Unary operators
>
> footnote 83)
> Among the invalid values for dereferencing a pointer
> by the unary * operator
> are a null pointer,

*x does not dereference a pointer; it converts it into an lvalue. That's why *x
= expr is allowed.

--
I remember finding out about you, | I survived XYZZY-Day.
Everyday my mind is all around you,| I'm whoever you want me to be.
Looking out from my lonely room |Annoying Usenet one post at a time.
Day after day. | At least I can stay in character.

pete

6/5/2011 12:25:00 PM

0

China Blue Angels wrote:
>
> In article <4DEB68F3.7550@mindspring.com>, pete <pfiland@mindspring.com> wrote:
>
> > China Blue Angels wrote:
> >
> > > *(int*)0 NULL address lvalue.
> >
> > No, that's just wrong.
> >
> > 6.5.3 Unary operators
> >
> > footnote 83)
> > Among the invalid values for dereferencing a pointer
> > by the unary * operator
> > are a null pointer,
>
> *x does not dereference a pointer; it converts it into an lvalue.

You just made that up.

--
pete

James Kuyper

6/5/2011 2:50:00 PM

0

On 06/05/2011 08:17 AM, China Blue Angels wrote:
> In article <4DEB68F3.7550@mindspring.com>, pete <pfiland@mindspring.com> wrote:
>
>> China Blue Angels wrote:
>>
>>> *(int*)0 NULL address lvalue.
>>
>> No, that's just wrong.
>>
>> 6.5.3 Unary operators
>>
>> footnote 83)
>> Among the invalid values for dereferencing a pointer
>> by the unary * operator
>> are a null pointer,
>
> *x does not dereference a pointer; it converts it into an lvalue. That's why *x
> = expr is allowed.

The standard does not use any form of the word "dereference" in any
other part of the standard, except that footnote, so it's arguable what
is meant by that word. However, that footnote is referred to by
6.5.3.2p4: "... If an invalid value has been assigned to the pointer,
the behavior of the unary * operator is undefined.87)"

It is the unary * operator itself which has undefined behavior, not the
assignment operator when it is assigned to. If "the invalid values for
dereferencing a pointer" does not directly refer to "an invalid value"
for which "the behavior of the unary * operator is undefined", then what
is that footnote doing in that position?

In any event, footnotes are not normative; it's not the footnote that
makes the behavior undefined, it merely points out something that can be
derived from the normative text.

6.5.3.2p4 defines the behavior of the unary * operator "If the operand
points to a function...", and "... if it it point to an object ...", but
it provides no definition of the behavior in any other case. A null
pointer doesn't point at anything, neither a function, nor an object, so
the behavior of the unary * operator on such a pointer is undefined "by
the omission of any explicit definition of behavior." (4p2). There are
exceptions when it is not evaluated at all. That occurs when it is the
operand of a & or sizeof operators (6.5.3.2p4 and 6.5.3.4p2, respectively).
--
James Kuyper

LJ

6/5/2011 3:36:00 PM

0

On Jun 5, 4:29 pm, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
> On 6/5/2011 6:17 AM, LJ wrote:
>
> > On Jun 5, 2:04 pm, Tobias Blass<tobiasbl...@gmx.net>  wrote:
> >> On 2011-06-05, LJ<djtheshowstop...@gmail.com>  wrote:
>
> >>> ) I have a doubt that why does the below expression  not lead to null
> >>> pointer indirection error:
>
> >>> &(*(int * )0);
>
> >>> It runs successfully and evaluates to zero.
>
> >> Perhaps the compiler optimized it to (int*)0.
>
> > So is it implementation-dependent to give an error or not??
>
>      Not implementation-dependent.  When the operand of the address-of
> operator & is the result of applying the indirection operator *, the
> two "cancel each other out" and neither is evaluated.  (Section
> 6.5.3.2 paragraph 3 of the C Standard, if you'd like to look it up.)
>
> > An implementation of offsetof() macro uses a technique similar to this
>
>      You haven't shown the implementation, but I suspect that if you
> examine it closely you'll find there's a little more going on than
> just "& swallows *."
>
> --
> Eric Sosman
> esos...@ieee-dot-org.invalid

The implementation I am referring is http://c-faq.com/struct/off...
My doubt is why does accessing the structure member mem using a null
pointer not lead to an error??