Re: storage for temporaries



Netocrat wrote:
>Old Wolf wrote:
>> Netocrat wrote:
>>>
>>> In the original example, if sfunc().a is legal
>>> (where "a" is an array), then "a" decays to a pointer
>>
>> OK so far...
>>
>>> and therefore the storage
>>> location of the struct returned by sfunc() can be identified.
>>
>> How would you identify it? Recall Lawrence Kirby's quote:
>>
>> "... If an attempt is made to modify the result of a function
>> call or to access it after the next sequence point, the behavior
>> is undefined."
>
> Substitute "must exist" for "can be identified" then. i.e. the pointer
> value must exist, regardless of whether we can identify what it is.
>
>> So you can't printf this pointer.
>
> Agreed.
>
>> You can't do a less-than or greater-than on it either (since
>> that requires two pointers to the same object, and here we
>> allegedly have no pointers to ojbects!).
>
> Why can't you compare against an arbitrary object?

Because of 6.5.8#5 ("Relational operators") which is too large
to quote, but says informally that a relational comparison of
two pointers is undefined behaviour unless they point to the
same object (or one-past-the-end etc.)

>
> struct s { int a[1]; } s;
> struct s sfunc();
>
> if (s.a > sfunc().a)

A good example of this UB.
As for a rationale, I suppose it is unclear how to compare
pointers on a system that doesn't have a flat memory model.
They could have at least made it unspecified behaviour though.

> The comparison could be changed to any other operator including ==,
> although it is debatable whether that would be allowed to compare true.

The rules for == are different, in that you can compare
pointers to different objects. However I think that this
pointer we are discussing, would not be allowed to compare
equal to anything. I suppose that it could be the same
memory location as the actual object in the function that
has just returned, but there is no way that legal code can
verify this (barring human introspection).

.



Relevant Pages

  • Re: far pointer
    ... >they will compare equal regardless of whether or not ... If two pointers *that you could create without invoking undefined ... behavior* point to the same location, they will compare equal ... regardless of whether or not the bit patterns are the same. ...
    (comp.lang.c)
  • Re: Pointer equality and dereferencing
    ... a representation representing two different values. ... If a pointer to an object and a pointer past an array compare equal, ... a different array object that happens to immediately follow the first ... the attempt to verify that the pointers happen to have the ...
    (comp.std.c)
  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... The standard form is like this, ... 1108 The sizeof operator ... ... being able to compare unrelated pointers, ... So long as the pointers are of the same type, ...
    (comp.lang.c)
  • Re: STL algorithms & operator overloading
    ... > I'm trying to sort vector of pointers to classes using STL ... do is to write a suitable function to compare two pointers to object by ...
    (alt.comp.lang.learn.c-cpp)