Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Why are there always boycotts? Shouldn't there be girlcotts too? -- argon on #Linux


devel / comp.lang.c / Re: C aggregate initialization sequencing

SubjectAuthor
* C aggregate initialization sequencingAndrey Tarasevich
+* Re: C aggregate initialization sequencingSams Lara
|`- Re: C aggregate initialization sequencingAndrey Tarasevich
+- Re: C aggregate initialization sequencingKaz Kylheku
+- Re: C aggregate initialization sequencingAndrey Tarasevich
`* Re: C aggregate initialization sequencingGuillaume
 +* Re: C aggregate initialization sequencingKeith Thompson
 |+* Re: C aggregate initialization sequencingManfred
 ||`* Re: C aggregate initialization sequencingAlbert Arkwright
 || `- Re: C aggregate initialization sequencingKeith Thompson
 |`- Re: C aggregate initialization sequencingGuillaume
 `- Re: C aggregate initialization sequencingAndrey Tarasevich

1
C aggregate initialization sequencing

<t3n6ms$ejr$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21302&group=comp.lang.c#21302

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: C aggregate initialization sequencing
Date: Tue, 19 Apr 2022 13:39:54 -0700
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <t3n6ms$ejr$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Apr 2022 20:39:56 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="03fc7fe9c44de50fa3f970b3ed644889";
logging-data="14971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/5Lpvg+1n6Ux7CO/3kWdHD"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.0
Cancel-Lock: sha1:AqlliJCGKvYoyfrbXUTf4Vtl8ug=
Content-Language: en-US
 by: Andrey Tarasevich - Tue, 19 Apr 2022 20:39 UTC

Consider this

int main(void)
{
int a[10] = { a[5] = 42 };
}

What is supposed to happen in the above initialization? Is the standard
sufficiently specified to tell? Is it undefined behavior?

I see that C standard says that the actual initializer expressions are
indeterminately sequenced with regard to each other (and their ordering
is not necessarily the same as the order in which the initializer values
are actually stored into the destination object). But it is not clear
how this interoperates with the implied zero-initialization of the tail
portion of the array.

It is clear (assuming for a second there's no UB in the above) that the
above declaration actually initializes `a[0]`. But what about `a[5]`?

I see that most compilers end up with `42` in `a[0]` and `a[5]`. This is
natural to expect since their intended semantics is probably to
zero-initialize the whole array _before_ performing any other
initialization. But I suspect that it is theoretically possible that
some implementation might decide to fill the tail (or the "holes", in
more general case) with zeros _after_ performing all explicit
initializations. This latter approach would result in zero in `a[5]`.

Or does the lack of sequencing here leads to UB?

P.S. I seem to vaguely recall that "tricks" like these (i.e. assigning
or even referring to the object being declared in its own initializer)
were outlawed or planned to be outlawed somewhere somehow, but I don't
remember where and how. C++ perhaps?

--
Best regards,
Andrey Tarasevich

Re: C aggregate initialization sequencing

<t3ncr3$lf18$1@news.mixmin.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21308&group=comp.lang.c#21308

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!news.mixmin.net!.POSTED!not-for-mail
From: samlara...@gmail.com (Sams Lara)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Tue, 19 Apr 2022 23:18:18 +0100
Organization: Microsoft Unofficial Representative on Newsgroups
Message-ID: <t3ncr3$lf18$1@news.mixmin.net>
References: <t3n6ms$ejr$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 19 Apr 2022 22:24:35 -0000 (UTC)
Injection-Info: news.mixmin.net; posting-host="48d11a3b2fa45924111005cb39b9d63a5dfc88a3";
logging-data="703528"; mail-complaints-to="abuse@mixmin.net"
In-Reply-To: <t3n6ms$ejr$1@dont-email.me>
Content-Language: en-US
 by: Sams Lara - Tue, 19 Apr 2022 22:18 UTC

On 19/04/2022 21:39, Andrey Tarasevich wrote:
>
>
> I see that most compilers end up with `42` in `a[0]` and `a[5]`.

C++ in Visual Studio (std 14, 17 and 20) gives just a[0] = 42. The
program I wrote { slightly different from the norm} is this (This is not
vector declaration either;  No vector header is included in the program
but vector commands can be used to insert elements or delete elements ):

#include <iostream>
#include <array>

using namespace std;

int main(void)
{ array<int, 10> a = { a[5] = 42};

for (int i = 0; i < 10; i++)
{
cout << a[i] << " ";
}
cout << endl;
return 0;
}

clang command line issues a warning " variable 'a' is uninitialized when
used within its own initialization ";

GCC also gives some/lots errors that I didn't bother to read!!

I always thought that initialising the first item in an array is always
mandatory before initialising other items. However, I don't read
standards and I don't have a copy either. I just use books and "Common
Sense & Logic". I know this is a loaded statement and I have no interest
to argue about it here!

Re: C aggregate initialization sequencing

<t3nif8$t7n$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21310&group=comp.lang.c#21310

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Tue, 19 Apr 2022 17:00:39 -0700
Organization: A noiseless patient Spider
Lines: 18
Message-ID: <t3nif8$t7n$2@dont-email.me>
References: <t3n6ms$ejr$1@dont-email.me> <t3ncr3$lf18$1@news.mixmin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 20 Apr 2022 00:00:40 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f099378cd8c9dbcee77dd8e9b384fc53";
logging-data="29943"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ibVq8VEgpTTNbZrvHLI1r"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.0
Cancel-Lock: sha1:Fp8tkh0hlRh0jye0P7sWUApzM3c=
In-Reply-To: <t3ncr3$lf18$1@news.mixmin.net>
Content-Language: en-US
 by: Andrey Tarasevich - Wed, 20 Apr 2022 00:00 UTC

On 4/19/2022 3:18 PM, Sams Lara wrote:
> On 19/04/2022 21:39, Andrey Tarasevich wrote:
>>
>>
>> I see that most compilers end up with `42` in `a[0]` and `a[5]`.
>
>
> C++ in Visual Studio (std 14, 17 and 20) gives just a[0] = 42. The
>
>     array<int, 10> a = { a[5] = 42};

A class with user-defined initialization semantics is not a good fit in
the context of the original question.

--
Best regards,
Andrey Tarasevich

Re: C aggregate initialization sequencing

<20220419182217.721@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21313&group=comp.lang.c#21313

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Wed, 20 Apr 2022 01:25:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20220419182217.721@kylheku.com>
References: <t3n6ms$ejr$1@dont-email.me>
Injection-Date: Wed, 20 Apr 2022 01:25:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="328c6955eaea4a2d6cff2d3767937281";
logging-data="23991"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19x0iGFHjUbvopsQoGnDE5TOQrwy4064NY="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:KrKJsOu0XyEPgqS/pfMQSH+3+hw=
 by: Kaz Kylheku - Wed, 20 Apr 2022 01:25 UTC

On 2022-04-19, Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
> Consider this
>
> int main(void)
> {
> int a[10] = { a[5] = 42 };
> }
>
> What is supposed to happen in the above initialization? Is the standard
> sufficiently specified to tell? Is it undefined behavior?

The location a[5] is both zero-initialized, and assigned.

Can we infer that these two actions are separated by sequencing?

If not, it looks like a pretty clear cut case of an object modified
twice, just like a[5] = a[5]++;

Re: C aggregate initialization sequencing

<t3rtc5$abl$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21351&group=comp.lang.c#21351

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 08:31:15 -0700
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <t3rtc5$abl$1@dont-email.me>
References: <t3n6ms$ejr$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 21 Apr 2022 15:31:18 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1354408e1d2d121911005963ec7052b6";
logging-data="10613"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+r/lf8r12jrpHLAb3XjjTY"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:4YzLjGXHoMmms6+kpPSVz/lTzgY=
In-Reply-To: <t3n6ms$ejr$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Thu, 21 Apr 2022 15:31 UTC

On 4/19/2022 1:39 PM, Andrey Tarasevich wrote:
> Consider this
>
> int main(void)
> {
> int a[10] = { a[5] = 42 };
> }
> ...
> I see that most compilers end up with `42` in `a[0]` and `a[5]`. This is
> natural to expect since their intended semantics is probably to
> zero-initialize the whole array _before_ performing any other
> initialization. But I suspect that it is theoretically possible that
> some implementation might decide to fill the tail (or the "holes", in
> more general case) with zeros _after_ performing all explicit
> initializations. This latter approach would result in zero in `a[5]`.
>

My attempts to persuade the compiler to abandon this "pre-initailize
with zeroes" approach in an attempt to make it override that `42` in
`a[5]` proved unsuccessful so far.

Anyway, it is interesting to note though that GCC seems to be especially
indifferent to performance-related considerations in such situations.
When faced with code like this

struct S { int a[16384]; };

struct X { struct S s; int b[8]; };

int foo(struct S *s)
{
struct X x = { *s, { x.b[3] = 42 } };
...
}

it generates the following initialization code for the local `x` in -O3 mode

mov edx, 65568
xor esi, esi
mov rdi, rsp
call memset
mov rsi, rbp
mov rdi, rsp
mov edx, 65536
call memcpy
mov DWORD PTR [rsp+65548], 42
mov DWORD PTR [rsp+65536], 42

So it effectively initializes almost entire `x`s memory twice: first
time `memset`ing it to all-zeros, then again by `memcpy`ing over it from
`*s`.

Clang's -O3 is less performance-wasteful

mov rsi, rdi
mov rdi, rsp
mov edx, 65536
call memcpy@PLT
xorps xmm0, xmm0
movups xmmword ptr [rsp + 65540], xmm0
movups xmmword ptr [rsp + 65552], xmm0
mov dword ptr [rsp + 65548], 42
mov dword ptr [rsp + 65536], 42

It doesn't waste time zeroing-out memory that will be `memcpy`ed over
anyway. It makes sure to pre-zero-out `s.b` only.

--
Best regards,
Andrey Tarasevich

Re: C aggregate initialization sequencing

<t3s6nc$73g$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21362&group=comp.lang.c#21362

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!WrBle+l1O+AlWgca0dBD+g.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 20:10:48 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t3s6nc$73g$1@gioia.aioe.org>
References: <t3n6ms$ejr$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="7280"; posting-host="WrBle+l1O+AlWgca0dBD+g.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Thu, 21 Apr 2022 18:10 UTC

Le 19/04/2022 à 22:39, Andrey Tarasevich a écrit :
> Consider this
>
>   int main(void)
>   {
>     int a[10] = { a[5] = 42 };
>   }
>
> What is supposed to happen in the above initialization? Is the standard
> sufficiently specified to tell? Is it undefined behavior?

The syntax I knew, and that is mentioned in the standard is this:

int a[10] = { [5] = 42 };

I didn't even know you could add an identifier before the [] in the
initializer. I haven't seen this mentioned in the standard, but
apparently GCC (and some other compilers) do accept it. Not sure where
that comes from, or if I missed it in the standard (if so, please
someone point me to the right paragraph.)

Anyway, incomplete initializers are supposed to have implicit zeros for
the non-specified parts, so the above I mentioned:

int a[10] = { [5] = 42 };

will initialize 'a' with all zeros except a[5], which will be 42.

Interestingly, your variant:

int a[10] = { a[5] = 42 };

will initialize 'a' with all zeros except a[0] and a[5], which will both
be 42. (Tried with GCC, but apparently confirmed by others.)

Thing is, I personally don't know what "{ a[5] = 42 }" as an initializer
is supposed to mean. Not what you had in mind, surely.

Use: "int a[10] = { [5] = 42 };" as I suggested and as is shown in the C
standard, and it will do what is expected.

Again, if anyone can point me to a reason why "int a[10] = { a[5] = 42
};" can be considered correct syntax in any way, please enlighten me!

Re: C aggregate initialization sequencing

<87pmlaclym.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21366&group=comp.lang.c#21366

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 11:47:45 -0700
Organization: None to speak of
Lines: 71
Message-ID: <87pmlaclym.fsf@nosuchdomain.example.com>
References: <t3n6ms$ejr$1@dont-email.me> <t3s6nc$73g$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="b2543598fb29db122071009eef06e5da";
logging-data="12326"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BpORk6TyL0j8XW5YWNf6R"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:kjoBSmgf5xs5TrGoCf7ILsEMLUE=
sha1:hWQ20tHSF2mem3AUB2guKgWisu8=
 by: Keith Thompson - Thu, 21 Apr 2022 18:47 UTC

Guillaume <message@bottle.org> writes:
> Le 19/04/2022 à 22:39, Andrey Tarasevich a écrit :
>> Consider this
>>   int main(void)
>>   {
>>     int a[10] = { a[5] = 42 };
>>   }
>> What is supposed to happen in the above initialization? Is the
>> standard sufficiently specified to tell? Is it undefined behavior?
>
> The syntax I knew, and that is mentioned in the standard is this:
>
> int a[10] = { [5] = 42 };
>
> I didn't even know you could add an identifier before the [] in the
> initializer. I haven't seen this mentioned in the standard, but
> apparently GCC (and some other compilers) do accept it. Not sure where
> that comes from, or if I missed it in the standard (if so, please
> someone point me to the right paragraph.)

This:

int a[10] = { [5] = 42 };

uses a designated initializer (a feature added in C99 IIRC).
It specifies that the initializer 42 is used to initialize element
5 of the array.

This:

int a[10] = { a[5] = 42 };

is just a conventional initializer. The expression `a[5] = 42` is the
initializer, and its value is used to initialize a[0]. It's *not* a
designated initializer with an identifier added; it's just an
expression. (And the fact that it has a side effect causes the problem
being discussed here.)

The issue being discussed in the order of evaluation. If you wrote:

int a[10] = { expr };

where `expr` is some expression with no side effects, the semantics are
straightforward; the result of evaluating `expr` is used to initialize
a[0], and the other elements are initialized to zero. But if `expr` has
the side effect of assigning a value to a[5], it's not clear whether the
assignment to a[5] specified by the evaluation of the expression, or the
assignment to a[5] that happens implicitly when all elements other than
the first are set to zero, happens first. If those two updates are not
sequenced (with the language specifying which one happens first), then
the behavior is undefined, just as for something like `i++ + i++`.

Looking at the relevant section of the standard, I see:

The evaluations of the initialization list expressions are
indeterminately sequenced with respect to one another and thus the
order in which any side effects occur is unspecified.

It's not 100% clear, but I think that implies that the behavior is
undefined.

If you're writing code that's intended to be useful, don't write code
like that. If you want to initialize elements 0 and 5 to 42, just
write:

int a[10] = { [0] = 42, [5] = 42 };

--
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 */

Re: C aggregate initialization sequencing

<t3sa8u$1s1t$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21367&group=comp.lang.c#21367

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 21:11:26 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t3sa8u$1s1t$1@gioia.aioe.org>
References: <t3n6ms$ejr$1@dont-email.me> <t3s6nc$73g$1@gioia.aioe.org>
<87pmlaclym.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="61501"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.0
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Thu, 21 Apr 2022 19:11 UTC

On 4/21/2022 8:47 PM, Keith Thompson wrote:
> Guillaume <message@bottle.org> writes:
>> Le 19/04/2022 à 22:39, Andrey Tarasevich a écrit :
>>> Consider this
>>>   int main(void)
>>>   {
>>>     int a[10] = { a[5] = 42 };
>>>   }
<snip>
>
> If you're writing code that's intended to be useful, don't write code
> like that. If you want to initialize elements 0 and 5 to 42, just
> write:
>
> int a[10] = { [0] = 42, [5] = 42 };
>

That's the same thing I have been thinking along this thread.
I might see /some/ point if the question were like:

int n = 5; // or anything appropriately runtime-valued
int a[10] = { a[n] = 42 };

But even then, I believe the proper thing to do is:

int n = 5; // same as above
int a[10] = { 42 };
a[n] = a[0];

and let the compiler and optimizer do their job.

(For the record, I think too that the OP is UB)

Re: C aggregate initialization sequencing

<t3sceb$rjk$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21368&group=comp.lang.c#21368

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!WrBle+l1O+AlWgca0dBD+g.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 21:48:24 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t3sceb$rjk$1@gioia.aioe.org>
References: <t3n6ms$ejr$1@dont-email.me> <t3s6nc$73g$1@gioia.aioe.org>
<87pmlaclym.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="28276"; posting-host="WrBle+l1O+AlWgca0dBD+g.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Thu, 21 Apr 2022 19:48 UTC

Le 21/04/2022 à 20:47, Keith Thompson a écrit :
> Guillaume <message@bottle.org> writes:
>> Le 19/04/2022 à 22:39, Andrey Tarasevich a écrit :
>>> Consider this
>>>   int main(void)
>>>   {
>>>     int a[10] = { a[5] = 42 };
>>>   }
>>> What is supposed to happen in the above initialization? Is the
>>> standard sufficiently specified to tell? Is it undefined behavior?
>>
>> The syntax I knew, and that is mentioned in the standard is this:
>>
>> int a[10] = { [5] = 42 };
>>
>> I didn't even know you could add an identifier before the [] in the
>> initializer. I haven't seen this mentioned in the standard, but
>> apparently GCC (and some other compilers) do accept it. Not sure where
>> that comes from, or if I missed it in the standard (if so, please
>> someone point me to the right paragraph.)
>
> This:
>
> int a[10] = { [5] = 42 };
>
> uses a designated initializer (a feature added in C99 IIRC).
> It specifies that the initializer 42 is used to initialize element
> 5 of the array.
>
> This:
>
> int a[10] = { a[5] = 42 };
>
> is just a conventional initializer. The expression `a[5] = 42` is the
> initializer, and its value is used to initialize a[0].

Ah yes, sure!
It was looking so much like something that wasn't intended that I didn't
think of this.

I don't think it's normal that compilers (at least GCC) won't issue a
warning for this though. An expression assignment of a variable in its
initializer sounds pretty odd and probably undefined behavior?

Re: C aggregate initialization sequencing

<t3sedl$pi0$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21369&group=comp.lang.c#21369

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: andreyta...@hotmail.com (Andrey Tarasevich)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 13:22:11 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <t3sedl$pi0$1@dont-email.me>
References: <t3n6ms$ejr$1@dont-email.me> <t3s6nc$73g$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 21 Apr 2022 20:22:14 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="ba446d9f21993de0bb22d18fcd7ed7bc";
logging-data="26176"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1iToiHKnXF4ESyUI5ereW"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.8.1
Cancel-Lock: sha1:k6XmguPeC8L0lbfxiwEA7s1+YPU=
In-Reply-To: <t3s6nc$73g$1@gioia.aioe.org>
Content-Language: en-US
 by: Andrey Tarasevich - Thu, 21 Apr 2022 20:22 UTC

On 4/21/2022 11:10 AM, Guillaume wrote:
> Le 19/04/2022 à 22:39, Andrey Tarasevich a écrit :
>> Consider this
>>
>>    int main(void)
>>    {
>>      int a[10] = { a[5] = 42 };
>>    }
>>
>> What is supposed to happen in the above initialization? Is the
>> standard sufficiently specified to tell? Is it undefined behavior?
>
> The syntax I knew, and that is mentioned in the standard is this:
>
> int a[10] = { [5] = 42 };
>

My original example was quite likely "inspired" by someone who tried to
remember syntax for designated initializers for array elements, but failed.

Nevertheless, my question is specifically about sequencing in

int a[10] = { a[5] = 42 };

I myself have no trouble remembering how to use designated initializers.
So, designated initializers is not what my question is about.

It is not about how to do things properly. It's is about whether the
standard unspecified/underspecified with regard to my specific example.
And if it is unspecified, whether it is intentional.

--
Best regards,
Andrey Tarasevich

Re: C aggregate initialization sequencing

<t3shrh$14bv$1@gioia.aioe.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21370&group=comp.lang.c#21370

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!nOqLI1EbCan+82M4Y2qJhQ.user.46.165.242.75.POSTED!not-for-mail
From: "Albert....@gmail.com (Albert Arkwright)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 22:18:26 +0100
Organization: Aioe.org NNTP Server
Message-ID: <t3shrh$14bv$1@gioia.aioe.org>
References: <t3n6ms$ejr$1@dont-email.me> <t3s6nc$73g$1@gioia.aioe.org>
<87pmlaclym.fsf@nosuchdomain.example.com> <t3sa8u$1s1t$1@gioia.aioe.org>
Reply-To: Albert.Arkwright@gmail.com
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="37247"; posting-host="nOqLI1EbCan+82M4Y2qJhQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Content-Language: en-cn
X-Notice: Filtered by postfilter v. 0.9.2
 by: Albert Arkwright - Thu, 21 Apr 2022 21:18 UTC

>
> (For the record, I think too that the OP is UB)
>

What is UB? Google can't tell me this.

Re: C aggregate initialization sequencing

<87ilr2cdgf.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=21371&group=comp.lang.c#21371

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: C aggregate initialization sequencing
Date: Thu, 21 Apr 2022 14:51:28 -0700
Organization: None to speak of
Lines: 13
Message-ID: <87ilr2cdgf.fsf@nosuchdomain.example.com>
References: <t3n6ms$ejr$1@dont-email.me> <t3s6nc$73g$1@gioia.aioe.org>
<87pmlaclym.fsf@nosuchdomain.example.com>
<t3sa8u$1s1t$1@gioia.aioe.org> <t3shrh$14bv$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="b2543598fb29db122071009eef06e5da";
logging-data="30971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180U3ovrkfEbCrrG7ATmCw8"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:LI2L1xa6ESteD+Lso+rayvPCLCU=
sha1:Yu6Q1O+/Gb+wW/1p5RJ9I5Llwc0=
 by: Keith Thompson - Thu, 21 Apr 2022 21:51 UTC

Albert Arkwright <"Albert.Arkwright "@gmail.com> writes:
>> (For the record, I think too that the OP is UB)
>
> What is UB? Google can't tell me this.

UB is undefined behavior, defined by the C standard as "behavior, upon
use of a nonportable or erroneous program construct or of erroneous
data, for which this International Standard imposes no requirements".

--
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 */

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor