Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #161959

Re: Array is an non-modifiable lvalue - where does it matter?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Array is an non-modifiable lvalue - where does it matter?
Date 2021-07-17 20:29 -0700
Organization None to speak of
Message-ID <87wnpo1eq9.fsf@nosuchdomain.example.com> (permalink)
References <scq5c5$tql$1@dont-email.me> <scsiq3$ens$1@dont-email.me> <scsk02$v8i$1@dont-email.me> <scvvrv$s1r$1@dont-email.me>

Show all headers | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
> On 7/16/2021 11:43 AM, Andrey Tarasevich wrote:
>> On 7/16/2021 11:22 AM, James Kuyper wrote:
>>>
>>> I have a vague memory that this wording is connected to a subtle issue
>>> with functions that return values of struct type, where the struct
>>> contains an array member. But I can't remember what the issue was.
>>>
>> The issue(s) stem from the combination of the following bits and
>> pieces:
>>    1. Function return is always an rvalue
>>    2. Member array of an rvalue struct object is also an rvalue
>>    3. The result of unary '*' is always an lvalue. And, say, `[]` is
>> defined through unary '*' and '+'.
>> 2 and 3 conflict with each other.
>> ...
>
>> C99 cops out of this latter problem by stating that the behavior is
>> undefined in this case.
>
> I see that I fell victim to the same confusion I'm carefully trying to
> avoid.
>
> Of course, there's no real conflict between 2 and 3. Value category is
> a property of an expression, not a property of an object. There's
> nothing wrong with having two different expressions that expose the
> same underlying object as an lvalue and as an rvalue at the same time.
>
> C89/90 apparently did not want to introduce the concept of "temporary
> lifetime". That's why it simply prohibited access to arrays inside 
> rvalue structs.

Unless I'm missing something, C89/C90 didn't *prohibit* access to arrays
inside rvalue structs.  The idea was simply overlooked (and it is rather
obscure).  It says that an array expression is converted to a pointer
expression that points to the initial element of "the array object",
overlooking the rare cases when there is no array object to point to.
It was a bug in the standard, fixed in C11.

> C99 decided that this is unnecessarily restrictive, and now we have
> lvalue access to elements of arrays inside rvalue structs. But still
> C99 is afraid to allow us to modify these elements. Granted, the use
> cases will always be pronouncedly contrived
>
>   #include <stdio.h>
>
>   struct S { int a[10]; };
>
>   struct S foo()
>   {
>     return (struct S) { 0 };
>   }
>
>   int main()
>   {
>     int *p;
>     p = foo().a, p[5] = 42, printf("%d\n", p[5]); // undefined behavior
>   }
>
> but in some corner cases it could have been useful...

That was resolved in C11, not C99.  C11 introduced the concept of
"temporary lifetime" for non-lvalue expressions of structure or union
type with an array member.  See N1570 6.2.4p8.

I can't think of any cases where allowing modification of an object with
temporary lifetime would be useful.  In your admittedly contrived
example, if you wanted the struct S object to be persistent, you could
arrange for that easily enough.

(Another approach might have been to add a rule saying that foo().a
yields a pointer of type `const int*`, but that's a bit ugly.)

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-15 13:21 -0700
  Re: Array is an non-modifiable lvalue - where does it matter? Öö Tiib <ootiib@hot.ee> - 2021-07-15 13:55 -0700
    Re: Array is an non-modifiable lvalue - where does it matter? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-15 22:12 +0100
      Re: Array is an non-modifiable lvalue - where does it matter? Öö Tiib <ootiib@hot.ee> - 2021-07-15 15:07 -0700
        Re: Array is an non-modifiable lvalue - where does it matter? David Brown <david.brown@hesbynett.no> - 2021-07-16 09:52 +0200
          Re: Array is an non-modifiable lvalue - where does it matter? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-16 02:31 -0700
          Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-16 10:59 -0700
            Re: Array is an non-modifiable lvalue - where does it matter? David Brown <david.brown@hesbynett.no> - 2021-07-17 16:23 +0200
              Re: Array is an non-modifiable lvalue - where does it matter? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-17 20:14 -0400
    Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-15 14:36 -0700
      Re: Array is an non-modifiable lvalue - where does it matter? Öö Tiib <ootiib@hot.ee> - 2021-07-15 15:19 -0700
  Re: Array is an non-modifiable lvalue - where does it matter? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-15 14:06 -0700
  Re: Array is an non-modifiable lvalue - where does it matter? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-16 14:22 -0400
    Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-16 11:43 -0700
      Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-17 18:24 -0700
        Re: Array is an non-modifiable lvalue - where does it matter? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-17 20:29 -0700
        Re: Array is an non-modifiable lvalue - where does it matter? Siri Cruise <chine.bleu@yahoo.com> - 2021-07-17 21:25 -0700
    Re: Array is an non-modifiable lvalue - where does it matter? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-16 15:22 -0700
  Re: Array is an non-modifiable lvalue - where does it matter? Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-16 22:21 +0000
    Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-16 23:27 -0700
      Re: Array is an non-modifiable lvalue - where does it matter? Kaz Kylheku <563-365-8930@kylheku.com> - 2021-07-17 16:53 +0000
        Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-17 10:49 -0700
        Re: Array is an non-modifiable lvalue - where does it matter? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-17 14:01 -0700
      Re: Array is an non-modifiable lvalue - where does it matter? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-18 02:09 +0100
    Re: Array is an non-modifiable lvalue - where does it matter? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-17 20:12 -0400
  Re: Array is an non-modifiable lvalue - where does it matter? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-07-17 13:20 -0700
    Re: Array is an non-modifiable lvalue - where does it matter? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-19 14:44 -0700
      Re: Array is an non-modifiable lvalue - where does it matter? "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-07-23 23:26 -0700

csiph-web