Re: Regarding assignment to struct

Liste des GroupesRevenir à cl c 
Sujet : Re: Regarding assignment to struct
De : already5chosen (at) *nospam* yahoo.com (Michael S)
Groupes : comp.lang.c
Date : 05. May 2025, 09:12:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250505111213.00004b55@yahoo.com>
References : 1 2 3 4
User-Agent : Claws Mail 3.19.1 (GTK+ 2.24.33; x86_64-w64-mingw32)
On Sun, 4 May 2025 22:22:12 -0700
Andrey Tarasevich <noone@noone.net> wrote:

On Sun 5/4/2025 6:48 AM, Tim Rentsch wrote:
>
One dark corner this feature has, is that in C (as opposed to C++)
the result of an assignment operator is an rvalue, which can
easily lead to some interesting consequences related to structs
with arrays inside. 
 
I'm curious to know what interesting consequences you mean here.  Do
you mean something other than cases that have undefined behavior? 
 
I'm referring to the matter of the address identity of the resultant
rvalue object. At first, "address identity of rvalue" might sound
strange, but the standard says that there's indeed an object tied to
such rvalue, and once we start applying array-to-pointer conversion
(and use `[]` operator), lvalues and addresses quickly come into the
picture.
 
The standard says in 6.2.4/8:
 
"A non-lvalue expression with structure or union type, where the
structure or union contains a member with array type [...]
refers to an object with automatic storage duration and temporary
lifetime. Its lifetime begins when the expression is evaluated and
its initial value is the value of the expression. Its lifetime ends
when the evaluation of the containing full expression ends. [...]
Such an object need not have a unique address."
https://port70.net/~nsz/c/c11/n1570.html#6.2.4p8
 
I wondering what the last sentence is intended to mean ("... need not
have a unique address"). At the first sight, the intent seems to be
obvious: it simply says that such temporary objects might repeatedly
appear (and disappear) at the same location in storage, which is a
natural thing to expect.
 
But is it, perhaps, intended to also allow such temporaries to have
addresses identical to regular named objects? It is not immediately
clear to me.
 
And when I make the following experiment with GCC and Clang
 
   #include <stdio.h>
 
   struct S { int a[10]; };
 
   int main()
   {
     struct S a, b = { 0 };
     int *pa, *pb, *pc;
 
     pa = &a.a[5];
     pb = &b.a[5];
     pc = &(a = b).a[5];
 
     printf("%p %p %p\n", pa, pb, pc);
   }
 
I consistently get the following output from GCC
 
   0x7fff73eb5544 0x7fff73eb5574 0x7fff73eb5544
 
And this is what I get from Clang
 
   0x7ffd2b8dbf44 0x7ffd2b8dbf14 0x7ffd2b8dbee4
 
As you can see, GCC apparently took C++-like approach to this
situation. The returned "temporary" is not really a separate
temporary at all, but actually `a` itself.
 
Meanwhile, in Clang all three pointers are different, i.e. Clang
decided to actually create a separate temporary object for the result
of the assignment.
 
I have a strong feeling that GCC's behavior is non-conforming. The
last sentence of 6.2.4/8 is not supposed to permit "projecting" the
resultant temporaries onto existing named objects. I could be wrong...
 

According to my understanding, you are wrong.
Taking pointer of non-lvalue is UB, so anything compiler does is
conforming.


Date Sujet#  Auteur
2 May 25 * Regarding assignment to struct84Lew Pitcher
2 May 25 +- Re: Regarding assignment to struct1Keith Thompson
2 May 25 +* Re: Regarding assignment to struct13Barry Schwarz
2 May 25 i`* That depends... (Was: Regarding assignment to struct)12Kenny McCormack
3 May 25 i `* Re: That depends... (Was: Regarding assignment to struct)11Lew Pitcher
3 May 25 i  +- Re: That depends... (Was: Regarding assignment to struct)1Lawrence D'Oliveiro
3 May 25 i  +- Re: That depends... (Was: Regarding assignment to struct)1Janis Papanagnou
3 May 25 i  +* Re: That depends... (Was: Regarding assignment to struct)5Kaz Kylheku
3 May 25 i  i+* Re: That depends... (Was: Regarding assignment to struct)3Kaz Kylheku
5 May 25 i  ii`* Re: That depends... (Was: Regarding assignment to struct)2Janis Papanagnou
5 May 25 i  ii `- Re: That depends... (Was: Regarding assignment to struct)1Kaz Kylheku
4 May 25 i  i`- Re: That depends... (Was: Regarding assignment to struct)1Tim Rentsch
3 May 25 i  +- Re: That depends... (Was: Regarding assignment to struct)1James Kuyper
4 May 25 i  `* Re: That depends... (Was: Regarding assignment to struct)2Tim Rentsch
4 May 25 i   `- Re: That depends... (Was: Regarding assignment to struct)1Lew Pitcher
2 May 25 +* Re: Regarding assignment to struct2Waldek Hebisch
3 May 25 i`- Re: Regarding assignment to struct1Lew Pitcher
3 May 25 +* Re: Regarding assignment to struct51Andrey Tarasevich
3 May 25 i+* Re: Regarding assignment to struct9Lawrence D'Oliveiro
4 May 25 ii`* Re: Regarding assignment to struct8Keith Thompson
4 May 25 ii `* Re: Regarding assignment to struct7James Kuyper
4 May 25 ii  +- Re: Regarding assignment to struct1Kenny McCormack
4 May 25 ii  +- Re: Regarding assignment to struct1David Brown
4 May 25 ii  `* Re: Regarding assignment to struct4Keith Thompson
5 May 25 ii   +- Re: Regarding assignment to struct1James Kuyper
5 May 25 ii   +- Re: Regarding assignment to struct1Keith Thompson
6 May 25 ii   `- Re: Regarding assignment to struct1Tim Rentsch
3 May 25 i+- Re: Regarding assignment to struct1Lawrence D'Oliveiro
4 May 25 i`* Re: Regarding assignment to struct40Tim Rentsch
5 May 25 i `* Re: Regarding assignment to struct39Andrey Tarasevich
5 May 25 i  +* Re: Regarding assignment to struct16Michael S
5 May 25 i  i+* Re: Regarding assignment to struct14Andrey Tarasevich
5 May 25 i  ii`* Re: Regarding assignment to struct13Michael S
5 May 25 i  ii +- Re: Regarding assignment to struct1Tim Rentsch
5 May 25 i  ii `* Re: Regarding assignment to struct11Andrey Tarasevich
5 May 25 i  ii  +* Re: Regarding assignment to struct2Michael S
6 May 25 i  ii  i`- Re: Regarding assignment to struct1Tim Rentsch
5 May 25 i  ii  `* Re: Regarding assignment to struct8Keith Thompson
6 May 25 i  ii   +* Re: Regarding assignment to struct2Tim Rentsch
6 May 25 i  ii   i`- Re: Regarding assignment to struct1Keith Thompson
6 May 25 i  ii   +- Re: Regarding assignment to struct1David Brown
6 May 25 i  ii   `* Re: Regarding assignment to struct4Waldek Hebisch
6 May 25 i  ii    +* Re: Regarding assignment to struct2David Brown
7 May 25 i  ii    i`- Re: Regarding assignment to struct1David Brown
6 May 25 i  ii    `- Re: Regarding assignment to struct1Keith Thompson
5 May 25 i  i`- Re: Regarding assignment to struct1Tim Rentsch
5 May 25 i  +* Re: Regarding assignment to struct4Keith Thompson
5 May 25 i  i`* Re: Regarding assignment to struct3Andrey Tarasevich
8 May 25 i  i `* Re: Regarding assignment to struct2Tim Rentsch
8 May 25 i  i  `- Re: Regarding assignment to struct1David Brown
5 May 25 i  +* Re: Regarding assignment to struct15Keith Thompson
5 May 25 i  i+* Re: Regarding assignment to struct6Michael S
5 May 25 i  ii+- Re: Regarding assignment to struct1Kenny McCormack
5 May 25 i  ii+* Re: Regarding assignment to struct3Keith Thompson
5 May 25 i  iii`* Re: Regarding assignment to struct2Kaz Kylheku
6 May 25 i  iii `- Re: Regarding assignment to struct1Tim Rentsch
6 May 25 i  ii`- Re: Regarding assignment to struct1Tim Rentsch
5 May 25 i  i`* Re: Regarding assignment to struct8Tim Rentsch
5 May 25 i  i `* Re: Regarding assignment to struct7Keith Thompson
6 May 25 i  i  `* Re: Regarding assignment to struct6Nick Bowler
6 May 25 i  i   `* Re: Regarding assignment to struct5Keith Thompson
7 May 25 i  i    `* Re: Regarding assignment to struct4Nick Bowler
7 May 25 i  i     +* Re: Regarding assignment to struct2Keith Thompson
8 May 25 i  i     i`- Re: Regarding assignment to struct1Nick Bowler
8 May 25 i  i     `- Re: Regarding assignment to struct1Tim Rentsch
5 May 25 i  +* Re: Regarding assignment to struct2Tim Rentsch
5 May 25 i  i`- Re: Regarding assignment to struct1David Brown
12 May 25 i  `- Re: Regarding assignment to struct1NotAorB
3 May 25 +* Re: Regarding assignment to struct8David Brown
5 May 25 i`* Re: Regarding assignment to struct7Muttley
5 May 25 i +- Re: Regarding assignment to struct1David Brown
5 May 25 i `* Re: Regarding assignment to struct5Keith Thompson
6 May 25 i  +- Re: Regarding assignment to struct1Muttley
6 May 25 i  +* Re: Regarding assignment to struct2David Brown
6 May 25 i  i`- Re: Regarding assignment to struct1Muttley
6 May 25 i  `- Re: Regarding assignment to struct1Michael S
4 May 25 +* Re: Regarding assignment to struct6Richard Damon
4 May 25 i`* Re: Regarding assignment to struct5Michael S
4 May 25 i +* Re: Regarding assignment to struct2Lawrence D'Oliveiro
4 May 25 i i`- Re: Regarding assignment to struct1David Brown
6 May 25 i +- Re: Regarding assignment to struct1Tim Rentsch
12 May 25 i `- Re: Regarding assignment to struct1Rosario19
4 May 25 +- Re: Regarding assignment to struct1Tim Rentsch
4 May 25 `- Re: Regarding assignment to struct1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal