Deutsch   English   Français   Italiano  
<veiuf0$15s23$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Thiago Adams <thiago.adams@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: c initialization algorithm
Date: Mon, 14 Oct 2024 08:14:40 -0300
Organization: A noiseless patient Spider
Lines: 77
Message-ID: <veiuf0$15s23$1@dont-email.me>
References: <vehee7$r8ri$1@dont-email.me> <20241013163050.897@kylheku.com>
 <86cyk3xoqw.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 14 Oct 2024 13:14:41 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7fecc5184c34083b703bea6d91efef95";
	logging-data="1241155"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+d/QJU3VlrO6BUBotnxRvxLN34tNVhYVw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gvy+596wFuzaFX1YrF/Mg2xeRWI=
In-Reply-To: <86cyk3xoqw.fsf@linuxsc.com>
Content-Language: en-US
Bytes: 3328

On 14/10/2024 02:00, Tim Rentsch wrote:
> Kaz Kylheku <643-408-1753@kylheku.com> writes:
> 
>> On 2024-10-13, Thiago Adams <thiago.adams@gmail.com> wrote:
>>
>>> The algorithm for C initialization as described in the standard
>>> and implemented in gcc allow this.
>>>
>>> struct X{
>>>       int a,b,c;
>>> };
>>>
>>> int main()
>>> {
>>>       struct X x = {.a=1,2,3,.a=1, 2, 3};
>>> }
>>>
>>> https://godbolt.org/z/7naedbEM6
>>>
>>> Basically, when a designed initializer is found the "cursor" goes
>>> to that member and the following members are initialized in
>>> order.
>>
>>   I do not suspect that therw is an observable order.  I.e. as
>>   in a required order considered observable behavior.
> 
> Have you checked to C standard to see what it says about that?

I think these parts are relevant.

"
Each brace-enclosed initializer list has an associated current object. 
When no designations are present, subobjects of the current object are 
initialized in order according to the type of the current object:
array elements in increasing subscript order, structure members in 
declaration order, and the first named member of a union.167) In 
contrast, a designation causes the following initializer to begin
initialization of the subobject described by the designator. 
Initialization then continues forward in order, beginning with the next 
subobject after that described by the designator.168)
"

The initialization shall occur in initializer list order, each 
initializer provided for a particular subobject
overriding any previously listed initializer for the same subobject;170) 
all subobjects that are not
initialized explicitly are subject to default initialization.
"

"170: Any initializer for the subobject which is overridden and so not 
used to initialize that subobject may not be evaluated at all. "


This last part made me create this sample in GCC. Reading it seems like 
it could potentially use the previous value.


#include <stdio.h>

struct X {
     int a, b;
};

int main()
{
   const struct X x2 = { .a=1, .b = x2.a, .a =2 };
   printf("a=%d b=%d", x2.a, x2.b); //a=2 b=2
}

https://godbolt.org/z/6K7xKqh86



clang shows when something is overridden.