Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Almost nothing in Perl serves a single purpose. -- Larry Wall in <199712040054.QAA13811@wall.org>


devel / comp.lang.c / Data Initialisation

SubjectAuthor
* Data Initialisationbart
+* Re: Data InitialisationKeith Thompson
|`* Re: Data Initialisationbart
| `- Re: Data InitialisationDavid Brown
+* Re: Data InitialisationTim Rentsch
|`* Re: Data InitialisationKeith Thompson
| +* Re: Data InitialisationTim Rentsch
| |`- Re: Data InitialisationKeith Thompson
| `- Who is in charge here? (Was: Data Initialisation)Kenny McCormack
+* Re: Data InitialisationThiago Adams
|`* Re: Data InitialisationDavid Brown
| `* Re: Data InitialisationMichael S
|  `- Re: Data InitialisationDavid Brown
`- Re: Data Initialisationbart

1
Data Initialisation

<urie91$2km5r$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Data Initialisation
Date: Mon, 26 Feb 2024 16:29:22 +0000
Organization: A noiseless patient Spider
Lines: 83
Message-ID: <urie91$2km5r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 26 Feb 2024 16:29:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8721302dc2883c48b2e64fb653a28420";
logging-data="2775227"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2JBlDzQz6ufOTtywRiNLKFB7sgopl1hY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:uJqG4G/g06WHkNqpsil1LivmMpE=
Content-Language: en-GB
 by: bart - Mon, 26 Feb 2024 16:29 UTC

One weak part of my C compiler is in initialisation:

* No runtime data is allowed for {...} data inside functions

* For nested structs and arrays, the pattern of {...} braces must
exactly match the type structure, no missing braces allowed. (So {0}
can't be used to zero a struct; only missing elements in arrays are allowed)

* No support for designated initialisers

* No support for compound literals (this is all part of the same aspect)

So I thought I'd bite the bullet and fix all this. Until I came across
section 6.7.9 (in my C99 draft standard, which is Initialisation).

Paragraphs p17 to p38 described the rules for braces and for designated
initialisers. I vaguely remember that it was complicated, I didn't
realise it was this complicated!

For example, p35 shows this example:

struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};

I couldn't grok it. I had to use this loop to find out what was going on:

for (int i=0; i<sizeof(w)/sizeof(w[0]); ++i) {
printf("(%d %d %d) %d\n", w[i].a[0], w[i].a[1], w[i].a[2], w[i].b);
}

What this means is that you can declare a data strucure of any
complexity, and initialise random, nested elements within a single set
of { ... } braces. Here's a simple example:

int A[][10] = {
[7][3] = 99
};

I didn't know you could combine designators like this (I've never seen
such examples), but I don't believe that is necessary. This example
could also be written like this:

int A[][10] = {
[7] = {[3] = 99}
};

This doesn't require combining designators, and here there is a better
match in the structure of the braces: '99' is inside 2 sets of braces
rather than 1. But there is a hidden danger here too:

int A[] = {
[someexpr] = 99
}

For a local array, 'someexpr' could be 20, or it could be 2000000, high
enough to cause stack overflow if you're lucky. Or it could just
silently create a far bigger array than expected.

Here's another example from p36:

int a[MAX] = {1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0};

p37 explains what it means, but not well. I think that the '6 4 2 0'
values follow element a[MAX-5]. It also suggests that is it possible to
define values multiple times:

int A[] = {[3]=1, [3]=2, [3]=3};

It can get even more hairy:

int A[] = {[3]=1, [3]=2, [3]=A[3]};

What is the value of A[3]? My guess was 2, but when I tried it, it was 0
with gcc, 2 with tcc, an error with DMC, and -108754328 with clang.

Presumably this is because it's using an uninitialised value of A[3]?
But A[3] IS initialised, at least twice!

This is all far more chaotic than I'd expected, and I decided not to
bother upgrading my compiler. I have trouble implementing something that
(1) I don't agree with because it's been poorly devised;(2) I don't
understand anyway.

Re: Data Initialisation

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.samoylyk.net!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Mon, 26 Feb 2024 09:04:22 -0800
Organization: None to speak of
Lines: 120
Message-ID: <87cysjw415.fsf@nosuchdomain.example.com>
References: <urie91$2km5r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="9466d30cc4509f5291c1ad69a98237cd";
logging-data="2791188"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+vq5P/bNU2jhs+x0n/Tf8J"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:idgX4HgxGqI4zBnXT3p2rw9IooY=
sha1:k7bgOnzTQapC1b1172JESArfvEU=
 by: Keith Thompson - Mon, 26 Feb 2024 17:04 UTC

bart <bc@freeuk.com> writes:
> One weak part of my C compiler is in initialisation:
>
> * No runtime data is allowed for {...} data inside functions

In other words, {...} initializers can only contain constant
expressions. I presume this means you allow:
int a = 42;
int b = a;
but not
int a = 42;
int b = {a};

> * For nested structs and arrays, the pattern of {...} braces must
> exactly match the type structure, no missing braces allowed. (So {0}
> can't be used to zero a struct; only missing elements in arrays are allowed)
>
> * No support for designated initialisers
>
> * No support for compound literals (this is all part of the same aspect)
>
> So I thought I'd bite the bullet and fix all this. Until I came across
> section 6.7.9 (in my C99 draft standard, which is Initialisation).

Out of curiousity, why are you using a C99 draft? Drafts of later
editions are available.

> Paragraphs p17 to p38 described the rules for braces and for
> designated initialisers. I vaguely remember that it was complicated, I
> didn't realise it was this complicated!
>
> For example, p35 shows this example:
>
> struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};

That seems clear enough to me. It initializes w[0].a to {1}, w[1].a[0]
to 2, and everything else to the default value of 0.

> I couldn't grok it. I had to use this loop to find out what was going on:
>
> for (int i=0; i<sizeof(w)/sizeof(w[0]); ++i) {
> printf("(%d %d %d) %d\n", w[i].a[0], w[i].a[1], w[i].a[2], w[i].b);
> }
>
> What this means is that you can declare a data strucure of any
> complexity, and initialise random, nested elements within a single set
> of { ... } braces. Here's a simple example:
>
> int A[][10] = {
> [7][3] = 99
> };

Yes.

> I didn't know you could combine designators like this (I've never seen
> such examples), but I don't believe that is necessary. This example
> could also be written like this:
>
> int A[][10] = {
> [7] = {[3] = 99}
> };

None of it is necessary. It's convenient. The convoluted examples are
a result of not imposing arbitrary restrictions. I believe the examples
in the standard are intended to clarify the rules and show what's
possible, not as examples of good coding style.

> This doesn't require combining designators, and here there is a better
> match in the structure of the braces: '99' is inside 2 sets of braces
> rather than 1. But there is a hidden danger here too:
>
> int A[] = {
> [someexpr] = 99
> }
>
> For a local array, 'someexpr' could be 20, or it could be 2000000,
> high enough to cause stack overflow if you're lucky. Or it could just
> silently create a far bigger array than expected.

someexpr has to be a constant expression. You could as easily write:

int A[someexpr+1];

Designated initializers do not introduce any new danger.

> Here's another example from p36:
>
> int a[MAX] = {1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0};
>
> p37 explains what it means, but not well. I think that the '6 4 2 0'
> values follow element a[MAX-5]. It also suggests that is it possible
> to define values multiple times:
>
> int A[] = {[3]=1, [3]=2, [3]=3};

Yes.

> It can get even more hairy:
>
> int A[] = {[3]=1, [3]=2, [3]=A[3]};
>
> What is the value of A[3]? My guess was 2, but when I tried it, it was
> 0 with gcc, 2 with tcc, an error with DMC, and -108754328 with clang.
>
> Presumably this is because it's using an uninitialised value of A[3]?
> But A[3] IS initialised, at least twice!

"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."

> This is all far more chaotic than I'd expected, and I decided not to
> bother upgrading my compiler. I have trouble implementing something
> that (1) I don't agree with because it's been poorly devised;(2) I
> don't understand anyway.

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

Re: Data Initialisation

<urioka$2n3h4$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Mon, 26 Feb 2024 19:26:02 +0000
Organization: A noiseless patient Spider
Lines: 109
Message-ID: <urioka$2n3h4$1@dont-email.me>
References: <urie91$2km5r$1@dont-email.me>
<87cysjw415.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 26 Feb 2024 19:26:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8721302dc2883c48b2e64fb653a28420";
logging-data="2854436"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18wEMYIuNZdlxinwYL/DyPYMsuPLdzyAvU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Ilh31fmhT1JnCaZHCixKWAfrhkA=
Content-Language: en-GB
In-Reply-To: <87cysjw415.fsf@nosuchdomain.example.com>
 by: bart - Mon, 26 Feb 2024 19:26 UTC

On 26/02/2024 17:04, Keith Thompson wrote:
> bart <bc@freeuk.com> writes:
>> One weak part of my C compiler is in initialisation:
>>
>> * No runtime data is allowed for {...} data inside functions
>
> In other words, {...} initializers can only contain constant
> expressions. I presume this means you allow:
> int a = 42;
> int b = a;
> but not
> int a = 42;
> int b = {a};

Actually, that one works. This won't work:

int c[2] = {a, b};

> Out of curiousity, why are you using a C99 draft? Drafts of later
> editions are available.

C99 is fine. There's little new in later versions that concerns my compiler.

>> Paragraphs p17 to p38 described the rules for braces and for
>> designated initialisers. I vaguely remember that it was complicated, I
>> didn't realise it was this complicated!
>>
>> For example, p35 shows this example:
>>
>> struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};
>
> That seems clear enough to me.

Well not to me, even after I'd got over my surprise at seeing 3
designators in a row.

> It initializes w[0].a to {1}, w[1].a[0]
> to 2, and everything else to the default value of 0.

w[0].a = {1, 0, 0};
w[1].a = {2, 0, 0};

That is clearer. The only thing that elaborate initialiser adds, apart
from confusion, is info about the maximum dimension of w.

> None of it is necessary. It's convenient.

You don't get my point. Designators were added for their convenience,
but that could have been done in a simple manner. It didn't need to
complicate it by allowing arbitrarily long chains of designators:

[3].a.b.c[4].d = x

VLAs could also have been done in a far simpler manner (by making the
VLA an attribute of a variable, not a type).

That would have sufficed for nearly all (perhaps even all) of the uses
I've ever seen of designated initialisers and VLAs.

> The convoluted examples are
> a result of not imposing arbitrary restrictions. I believe the examples
> in the standard are intended to clarify the rules and show what's
> possible, not as examples of good coding style.

But it means implementators have to ensure those examples work.

An example like this:

int A[][10]= {
[4][3]=10,
[7][5]=20
};

works in gcc and tcc. But older compilers like DMC and lccwin have
trouble with it (they work with single-designator examples).

> someexpr has to be a constant expression. You could as easily write:
>
> int A[someexpr+1];
>
> Designated initializers do not introduce any new danger.

It means a rogue value could be somewhere in the hundred designators of
A's init data rather than in one place.

>> It can get even more hairy:
>>
>> int A[] = {[3]=1, [3]=2, [3]=A[3]};
>>
>> What is the value of A[3]? My guess was 2, but when I tried it, it was
>> 0 with gcc, 2 with tcc, an error with DMC, and -108754328 with clang.
>>
>> Presumably this is because it's using an uninitialised value of A[3]?
>> But A[3] IS initialised, at least twice!
>
> "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."

gcc with extra warnings said nothing about it. But this:

int A[2] = {A[0], A[1]};

did produce a warning.

Re: Data Initialisation

<86sf1eu178.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Mon, 26 Feb 2024 17:48:27 -0800
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <86sf1eu178.fsf@linuxsc.com>
References: <urie91$2km5r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c48ed012bb511246f927b8817f27d335";
logging-data="3004888"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+huK6fT548Mdyy+uIshxUP5QEX0MKloNI="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:SHcMaF0OBACz8w3WVdgvSxc6JRA=
sha1:CB+elY0I/4nLM1TxmCh5gAKT6Zw=
 by: Tim Rentsch - Tue, 27 Feb 2024 01:48 UTC

bart <bc@freeuk.com> writes:

> One weak part of my C compiler is in initialisation:

[...]

> * No support for designated initialisers
>
> So I thought I'd bite the bullet and fix all this.

Good show!

> Until I came across section 6.7.9 (in my C99 draft standard,
> which is Initialisation).
>
> Paragraphs p17 to p38 described the rules for braces and for
> designated initialisers. [...]
>
> What this means is that you can declare a data strucure of any
> complexity, and initialise random, nested elements within a
> single set of { ... } braces. Here's a simple example:
>
> int A[][10] = {
> [7][3] = 99
> };
>
> I didn't know you could combine designators like this (I've
> never seen such examples), but I don't believe that is
> necessary. This example could also be written like this:
>
> int A[][10] = {
> [7] = {[3] = 99}
> };

It's true that the overall initializers in these two declarations
have the same result. Note however that the individual
initializer

[7][3] = 99

and the individual initializer

[7] = {[3] = 99}

have different meanings, and are different in at least two
significant ways, depending on the context in which they appear.
If you have difficulty seeing what those differences are, I'm
sure Keith Thompson can explain what's going on. Depending on
circumstances, either the first way or the second way could be
a better choice; it depends on what one wants to accomplish
and to convey.

Re: Data Initialisation

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Mon, 26 Feb 2024 18:17:40 -0800
Organization: None to speak of
Lines: 60
Message-ID: <87v86aveez.fsf@nosuchdomain.example.com>
References: <urie91$2km5r$1@dont-email.me> <86sf1eu178.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="e411e02173c5727b001dab2051103785";
logging-data="3013358"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/VFvipy94NFyaWVvKn1v0u"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:ga7gS2WA0EShwh7GKOsWMB5UYNE=
sha1:OrP/pJ1oYwiZe48ExalJEP6GO4o=
 by: Keith Thompson - Tue, 27 Feb 2024 02:17 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> bart <bc@freeuk.com> writes:
>
>> One weak part of my C compiler is in initialisation:
>
> [...]
>
>> * No support for designated initialisers
>>
>> So I thought I'd bite the bullet and fix all this.
>
> Good show!
>
>> Until I came across section 6.7.9 (in my C99 draft standard,
>> which is Initialisation).
>>
>> Paragraphs p17 to p38 described the rules for braces and for
>> designated initialisers. [...]
>>
>> What this means is that you can declare a data strucure of any
>> complexity, and initialise random, nested elements within a
>> single set of { ... } braces. Here's a simple example:
>>
>> int A[][10] = {
>> [7][3] = 99
>> };
>>
>> I didn't know you could combine designators like this (I've
>> never seen such examples), but I don't believe that is
>> necessary. This example could also be written like this:
>>
>> int A[][10] = {
>> [7] = {[3] = 99}
>> };
>
> It's true that the overall initializers in these two declarations
> have the same result. Note however that the individual
> initializer
>
> [7][3] = 99
>
> and the individual initializer
>
> [7] = {[3] = 99}
>
> have different meanings, and are different in at least two
> significant ways, depending on the context in which they appear.
> If you have difficulty seeing what those differences are, I'm
> sure Keith Thompson can explain what's going on. Depending on
> circumstances, either the first way or the second way could be
> a better choice; it depends on what one wants to accomplish
> and to convey.

Tim, you don't get to make an assertion and then pass off the work
of explaining it to someone else.

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

Re: Data Initialisation

<86le76ttww.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Mon, 26 Feb 2024 20:25:51 -0800
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <86le76ttww.fsf@linuxsc.com>
References: <urie91$2km5r$1@dont-email.me> <86sf1eu178.fsf@linuxsc.com> <87v86aveez.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="c48ed012bb511246f927b8817f27d335";
logging-data="3178422"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gkrF5W1dX64w22uzvsVFACCMnE2DfhoQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:bxhtIslnTu6riJZfQX+uDF4IUyQ=
sha1:ygt0S1zDEWhf13Jra5vIkrtka3c=
 by: Tim Rentsch - Tue, 27 Feb 2024 04:25 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> bart <bc@freeuk.com> writes:
>>
>>> One weak part of my C compiler is in initialisation:
>>
>> [...]
>>
>>> * No support for designated initialisers
>>>
>>> So I thought I'd bite the bullet and fix all this.
>>
>> Good show!
>>
>>> Until I came across section 6.7.9 (in my C99 draft standard,
>>> which is Initialisation).
>>>
>>> Paragraphs p17 to p38 described the rules for braces and for
>>> designated initialisers. [...]
>>>
>>> What this means is that you can declare a data strucure of any
>>> complexity, and initialise random, nested elements within a
>>> single set of { ... } braces. Here's a simple example:
>>>
>>> int A[][10] = {
>>> [7][3] = 99
>>> };
>>>
>>> I didn't know you could combine designators like this (I've
>>> never seen such examples), but I don't believe that is
>>> necessary. This example could also be written like this:
>>>
>>> int A[][10] = {
>>> [7] = {[3] = 99}
>>> };
>>
>> It's true that the overall initializers in these two declarations
>> have the same result. Note however that the individual
>> initializer
>>
>> [7][3] = 99
>>
>> and the individual initializer
>>
>> [7] = {[3] = 99}
>>
>> have different meanings, and are different in at least two
>> significant ways, depending on the context in which they appear.
>> If you have difficulty seeing what those differences are, I'm
>> sure Keith Thompson can explain what's going on. Depending on
>> circumstances, either the first way or the second way could be
>> a better choice; it depends on what one wants to accomplish
>> and to convey.
>
> Tim, you don't get to make an assertion and then pass off the work
> of explaining it to someone else.

Do you agree that there are differences between the two
initializers?

Are you in fact able to explain what the differences
are?

If the answer to the preceding questions is Yes in
both cases then it seems we have no disagreement.

In any case you are under no obligation to provide
an explanation (and I never said you were). Nor am I.

Re: Data Initialisation

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Mon, 26 Feb 2024 20:45:49 -0800
Organization: None to speak of
Lines: 36
Message-ID: <87r0gyv7k2.fsf@nosuchdomain.example.com>
References: <urie91$2km5r$1@dont-email.me> <86sf1eu178.fsf@linuxsc.com>
<87v86aveez.fsf@nosuchdomain.example.com> <86le76ttww.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="e411e02173c5727b001dab2051103785";
logging-data="3185561"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18n9H1/THqHgUK7G0MG0oUt"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:5BC1/WxpnFe5X+JxztT0cRB+z54=
sha1:EoFhnY1gi8apHdGESoINjWonTfk=
 by: Keith Thompson - Tue, 27 Feb 2024 04:45 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
[...]
>>> If you have difficulty seeing what those differences are, I'm
>>> sure Keith Thompson can explain what's going on. Depending on
>>> circumstances, either the first way or the second way could be
>>> a better choice; it depends on what one wants to accomplish
>>> and to convey.
>>
>> Tim, you don't get to make an assertion and then pass off the work
>> of explaining it to someone else.
>
> Do you agree that there are differences between the two
> initializers?
>
> Are you in fact able to explain what the differences
> are?
>
> If the answer to the preceding questions is Yes in
> both cases then it seems we have no disagreement.

I didn't say we had a disagreement.

> In any case you are under no obligation to provide
> an explanation (and I never said you were). Nor am I.

Nobody said that anyone was obligated to do anything.

You're free to make assertions without explaining them, just as I'm free
to express my annoyance at the practice.

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

Re: Data Initialisation

<urkhr9$368rq$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Tue, 27 Feb 2024 12:42:32 +0100
Organization: A noiseless patient Spider
Lines: 205
Message-ID: <urkhr9$368rq$1@dont-email.me>
References: <urie91$2km5r$1@dont-email.me>
<87cysjw415.fsf@nosuchdomain.example.com> <urioka$2n3h4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 11:42:33 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b013b32b0ae31855296b19afd591ff2";
logging-data="3351418"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19k9JEMQtujwtvmWPnkxmoq/NlbIvJoMNk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:v1FaaRInr7vqYm9NlRWGL/10RUM=
In-Reply-To: <urioka$2n3h4$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Tue, 27 Feb 2024 11:42 UTC

On 26/02/2024 20:26, bart wrote:
> On 26/02/2024 17:04, Keith Thompson wrote:
>> bart <bc@freeuk.com> writes:
>>> One weak part of my C compiler is in initialisation:
>>>
>>> * No runtime data is allowed for {...} data inside functions
>>
>> In other words, {...} initializers can only contain constant
>> expressions.  I presume this means you allow:
>>      int a = 42;
>>      int b = a;
>> but not
>>      int a = 42;
>>      int b = {a};
>
> Actually, that one works. This won't work:
>
>     int c[2] = {a, b};

I don't know if this is practical for you in your compiler, but one way
to handle designated initialisers and non-constant initialisation would
be to treat it as first doing a zero initialisation of the entire
object, and then assignments. So given this:

int a = 42;
int b = a;
int c[2] = { a, b };

transform it into :

int a;
int b;
int c[2];

a = 42;
b = a;

memset(&c, 0, sizeof c);
c[0] = a;
c[1] = b;

Obviously clearing the whole object "c" and then assigning the values to
it is not the most efficient way to handle things if all the elements
are initialised. But it can be effective if only a few elements are
initialised explicitly, and it might be a relatively easy way to
implement something that works well enough.

>
>
>> Out of curiousity, why are you using a C99 draft?  Drafts of later
>> editions are available.
>
> C99 is fine. There's little new in later versions that concerns my
> compiler.

The big things in C11 are atomics and thread support, but those are
optional. But there's also a few small things that are nice - anonymous
structs and unions (supported by most compilers from long before C11,
but standardised in C11) and _Static_assert() are very useful (for me,
anyway). C17 is just a bug fix, but I think the typography improved so
it's nicer to refer to C17 documents :-) All in all, I don't think it
would be a large step to move from C99 to C17, if you skip the optional
parts. (And VLAs and complex numbers are mandatory in C99, but optional
in C11/C17.)

C23 is another matter - it has a /lot/ of new features, and is the
biggest change in C since C99.

>
>>> Paragraphs p17 to p38 described the rules for braces and for
>>> designated initialisers. I vaguely remember that it was complicated, I
>>> didn't realise it was this complicated!
>>>
>>> For example, p35 shows this example:
>>>
>>>      struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};
>>
>> That seems clear enough to me.
>
> Well not to me, even after I'd got over my surprise at seeing 3
> designators in a row.
>
>>  It initializes w[0].a to {1}, w[1].a[0]
>> to 2, and everything else to the default value of 0.
>
>     w[0].a = {1, 0, 0};
>     w[1].a = {2, 0, 0};
>
> That is clearer. The only thing that elaborate initialiser adds, apart
> from confusion, is info about the maximum dimension of w.
>
>
>> None of it is necessary.  It's convenient.
>
> You don't get my point. Designators were added for their convenience,
> but that could have been done in a simple manner. It didn't need to
> complicate it by allowing arbitrarily long chains of designators:
>
>     [3].a.b.c[4].d = x

Actually, it /does/. If you can't have arbitrarily long changes, you
have arbitrary restrictions on the chains. It would be like saying you
could write "int xs[2][3];" but not "int xs[2][3][5];", or that structs
could only be nested 4 levels. It's unlikely that people will want to
use deeply nested and long chains, but it's easier to say there are no
limits than to give limits.

(The C standard does give minimum "translation limits" for various
things, but they are aimed to be so large that no real code will hit
them, and few implementations have much in the way of arbitrary limits.)

>
> VLAs could also have been done in a far simpler manner (by making the
> VLA an attribute of a variable, not a type).

That would have been a slightly different thing. (I'm not saying it
would have been better or worse - I don't think it would make
significant practical difference in real code.) In some languages,
types are simpler and characteristics such as "const", "volatile", or
the size of an array are attributes of the variable. In some, they are
attributes of the type. That is the case for C. In C, if you write
"int xs[10];", then you are declaring a variable "xs" of type "int
[10]". The "[10]" is part of the type, not the variable. And thus the
same applies if you declare "int ys[n];".

I think a way to handle VLAs would be to treat :

int xs[X];

as :

const size_t xs_size = X;
int xs[xs_size];

It is possible that this is not quite consistent with the standards if
you have to evaluate "sizeof xs" and the expression "X" has
side-effects, but it would surely work for any normal code.

>
> That would have sufficed for nearly all (perhaps even all) of the uses
> I've ever seen of designated initialisers and VLAs.
>
>> The convoluted examples are
>> a result of not imposing arbitrary restrictions.  I believe the examples
>> in the standard are intended to clarify the rules and show what's
>> possible, not as examples of good coding style.
>
> But it means implementators have to ensure those examples work.
>
> An example like this:
>
>     int A[][10]= {
>         [4][3]=10,
>         [7][5]=20
>     };
>
> works in gcc and tcc. But older compilers like DMC and lccwin have
> trouble with it (they work with single-designator examples).
>

Emulate the ones that work :-)

>
>> someexpr has to be a constant expression.  You could as easily write:
>>
>>      int A[someexpr+1];
>>
>> Designated initializers do not introduce any new danger.
>
> It means a rogue value could be somewhere in the hundred designators of
> A's init data rather than in one place.

In either case, you have a mistake in the source leading to faults in
the generated code - I can't see a noticeable risk. And the alternative
to designated initialisers would be to declare the array, then write the
assignment "A[somexpr] = 99;" afterwards and have the bug there.

At least with designated initialisers you can easily catch the bug if
the array is declared with a specific size. And you can add a warning
to your compiler for local arrays exceeding a certain size.

>
>>> It can get even more hairy:
>>>
>>>      int A[] = {[3]=1, [3]=2, [3]=A[3]};
>>>
>>> What is the value of A[3]? My guess was 2, but when I tried it, it was
>>> 0 with gcc, 2 with tcc, an error with DMC, and -108754328 with clang.
>>>
>>> Presumably this is because it's using an uninitialised value of A[3]?
>>> But A[3] IS initialised, at least twice!
>>
>> "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."
>
> gcc with extra warnings said nothing about it. But this:
>
>     int A[2] = {A[0], A[1]};
>
> did produce a warning.
>
>

Re: Data Initialisation

<urkimd$36dg1$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: thiago.a...@gmail.com (Thiago Adams)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Tue, 27 Feb 2024 08:57:01 -0300
Organization: A noiseless patient Spider
Lines: 92
Message-ID: <urkimd$36dg1$1@dont-email.me>
References: <urie91$2km5r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 11:57:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c7e88d6c6460506a4df2b7b454c2be6d";
logging-data="3356161"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX182g8v++9y0WcO0Ah9WTrmmplhNz/RnlUo="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ZEV/90stqR1RNjfM7HwzahK2GwE=
In-Reply-To: <urie91$2km5r$1@dont-email.me>
Content-Language: en-US
 by: Thiago Adams - Tue, 27 Feb 2024 11:57 UTC

On 26/02/2024 13:29, bart wrote:
> One weak part of my C compiler is in initialisation:
>
> * No runtime data is allowed for {...} data inside functions
>
> * For nested structs and arrays, the pattern of {...} braces must
> exactly match the type structure, no missing braces allowed. (So {0}
> can't be used to zero a struct; only missing elements in arrays are
> allowed)
>
> * No support for designated initialisers
>
> * No support for compound literals (this is all part of the same aspect)
>
> So I thought I'd bite the bullet and fix all this. Until I came across
> section 6.7.9 (in my C99 draft standard, which is Initialisation).
>
> Paragraphs p17 to p38 described the rules for braces and for designated
> initialisers. I vaguely remember that it was complicated, I didn't
> realise it was this complicated!
>
> For example, p35 shows this example:
>
>     struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};
>
> I couldn't grok it. I had to use this loop to find out what was going on:
>
>   for (int i=0; i<sizeof(w)/sizeof(w[0]); ++i) {
>       printf("(%d %d %d) %d\n", w[i].a[0], w[i].a[1], w[i].a[2], w[i].b);
>   }
>
> What this means is that you can declare a data strucure of any
> complexity, and initialise random, nested elements within a single set
> of { ... } braces. Here's a simple example:
>
>     int A[][10] = {
>         [7][3] = 99
>     };
>
> I didn't know you could combine designators like this (I've never seen
> such examples), but I don't believe that is necessary. This example
> could also be written like this:
>
>     int A[][10] = {
>         [7] = {[3] = 99}
>     };
>
> This doesn't require combining designators, and here there is a better
> match in the structure of the braces: '99' is inside 2 sets of braces
> rather than 1. But there is a hidden danger here too:
>
>     int A[] = {
>        [someexpr] = 99
>     }
>
> For a local array, 'someexpr' could be 20, or it could be 2000000, high
> enough to cause stack overflow if you're lucky. Or it could just
> silently create a far bigger array than expected.
>
> Here's another example from p36:
>
>     int a[MAX] = {1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0};
>
> p37 explains what it means, but not well. I think that the '6 4 2 0'
> values follow element a[MAX-5]. It also suggests that is it possible to
> define values multiple times:
>
>     int A[] = {[3]=1, [3]=2, [3]=3};
>
> It can get even more hairy:
>
>     int A[] = {[3]=1, [3]=2, [3]=A[3]};
>
> What is the value of A[3]? My guess was 2, but when I tried it, it was 0
> with gcc, 2 with tcc, an error with DMC, and -108754328 with clang.
>
> Presumably this is because it's using an uninitialised value of A[3]?
> But A[3] IS initialised, at least twice!
>
>
> This is all far more chaotic than I'd expected, and I decided not to
> bother upgrading my compiler. I have trouble implementing something that
> (1) I don't agree with because it's been poorly devised;(2) I don't
> understand anyway.
>

I also want to understand better this.
I think the design must have some logic, I never stop to understand
this. I think the best way is see some generated code, because it is
maybe related with initialization performance. For instance, fill zeros
first then do the other initialization.

Who is in charge here? (Was: Data Initialisation)

<urkjmc$1rl21$1@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.chmurka.net!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Who is in charge here? (Was: Data Initialisation)
Date: Tue, 27 Feb 2024 12:14:04 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <urkjmc$1rl21$1@news.xmission.com>
References: <urie91$2km5r$1@dont-email.me> <86sf1eu178.fsf@linuxsc.com> <87v86aveez.fsf@nosuchdomain.example.com>
Injection-Date: Tue, 27 Feb 2024 12:14:04 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1954881"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 27 Feb 2024 12:14 UTC

In article <87v86aveez.fsf@nosuchdomain.example.com>,
Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
....
>Tim, you don't get to make an assertion and then pass off the work
>of explaining it to someone else.

Darn straight!

Tim must never be allowed to forget who makes the rules around here.

--
The last time a Republican cared about you, you were a fetus.

Re: Data Initialisation

<urkl1d$36vet$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Tue, 27 Feb 2024 13:37:00 +0100
Organization: A noiseless patient Spider
Lines: 130
Message-ID: <urkl1d$36vet$1@dont-email.me>
References: <urie91$2km5r$1@dont-email.me> <urkimd$36dg1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 27 Feb 2024 12:37:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b013b32b0ae31855296b19afd591ff2";
logging-data="3374557"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Tiia8f2PRXbpPgFDcNaSzQiTWd/guNmk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:O89l5uzuVPJnGla1VCbfrAVQDHE=
Content-Language: en-GB
In-Reply-To: <urkimd$36dg1$1@dont-email.me>
 by: David Brown - Tue, 27 Feb 2024 12:37 UTC

On 27/02/2024 12:57, Thiago Adams wrote:
> On 26/02/2024 13:29, bart wrote:
>> One weak part of my C compiler is in initialisation:
>>
>> * No runtime data is allowed for {...} data inside functions
>>
>> * For nested structs and arrays, the pattern of {...} braces must
>> exactly match the type structure, no missing braces allowed. (So {0}
>> can't be used to zero a struct; only missing elements in arrays are
>> allowed)
>>
>> * No support for designated initialisers
>>
>> * No support for compound literals (this is all part of the same aspect)
>>
>> So I thought I'd bite the bullet and fix all this. Until I came across
>> section 6.7.9 (in my C99 draft standard, which is Initialisation).
>>
>> Paragraphs p17 to p38 described the rules for braces and for
>> designated initialisers. I vaguely remember that it was complicated, I
>> didn't realise it was this complicated!
>>
>> For example, p35 shows this example:
>>
>>      struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};
>>
>> I couldn't grok it. I had to use this loop to find out what was going on:
>>
>>    for (int i=0; i<sizeof(w)/sizeof(w[0]); ++i) {
>>        printf("(%d %d %d) %d\n", w[i].a[0], w[i].a[1], w[i].a[2],
>> w[i].b);
>>    }
>>
>> What this means is that you can declare a data strucure of any
>> complexity, and initialise random, nested elements within a single set
>> of { ... } braces. Here's a simple example:
>>
>>      int A[][10] = {
>>          [7][3] = 99
>>      };
>>
>> I didn't know you could combine designators like this (I've never seen
>> such examples), but I don't believe that is necessary. This example
>> could also be written like this:
>>
>>      int A[][10] = {
>>          [7] = {[3] = 99}
>>      };
>>
>> This doesn't require combining designators, and here there is a better
>> match in the structure of the braces: '99' is inside 2 sets of braces
>> rather than 1. But there is a hidden danger here too:
>>
>>      int A[] = {
>>         [someexpr] = 99
>>      }
>>
>> For a local array, 'someexpr' could be 20, or it could be 2000000,
>> high enough to cause stack overflow if you're lucky. Or it could just
>> silently create a far bigger array than expected.
>>
>> Here's another example from p36:
>>
>>      int a[MAX] = {1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0};
>>
>> p37 explains what it means, but not well. I think that the '6 4 2 0'
>> values follow element a[MAX-5]. It also suggests that is it possible
>> to define values multiple times:
>>
>>      int A[] = {[3]=1, [3]=2, [3]=3};
>>
>> It can get even more hairy:
>>
>>      int A[] = {[3]=1, [3]=2, [3]=A[3]};
>>
>> What is the value of A[3]? My guess was 2, but when I tried it, it was
>> 0 with gcc, 2 with tcc, an error with DMC, and -108754328 with clang.
>>
>> Presumably this is because it's using an uninitialised value of A[3]?
>> But A[3] IS initialised, at least twice!
>>
>>
>> This is all far more chaotic than I'd expected, and I decided not to
>> bother upgrading my compiler. I have trouble implementing something
>> that (1) I don't agree with because it's been poorly devised;(2) I
>> don't understand anyway.
>>
>
> I also want to understand better this.
> I think the design must have some logic, I never stop to understand
> this. I think the best way is see some generated code, because it is
> maybe related with initialization performance. For instance, fill zeros
> first then do the other initialization.
>

The order of initialisation is by the order of the initialisers
(subject, as always, to the "as if" rule). So if you write :

int A[] = {[3]=1, [3]=2, [3]=3};

then the final "[3] = 3" is applied last, and overrides the others.
This does not seem very useful to me, as it stands. However, like many
features of C99, this has come about as a standardisation of gcc
extensions, and that is how the gcc syntax worked - therefore, C99
copied it for consistency with existing code. In gcc, it makes much
more sense because you can write :

int A[] = { [0 .. 9] = 1, [10 .. 29] = 2, [5] = 17 };

and use this feature to override a value given in a range.

<https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html>

The order of evaluation of the initialisers, by contrast, is
unspecified. It is also unspecified if overridden initialisers are
evaluated (for their side-effects) at all. Again, this is consistent
with the gcc extension, and it gives compilers more freedom in how they
organise the code. That does mean that code like this:

int A[] = {[3]=1, [3]=2, [3]=A[3]};

does not have a well-defined and consistent result. But then, neither
does "int x = x;", even though it is valid code. (It is sometimes used
as an idiom to tell static error checkers and other programmers that you
know "x" is not initialised, and that there should be no warning even if
it appears to be read before initialisation.)

Re: Data Initialisation

<20240227145950.00001d67@yahoo.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: already5...@yahoo.com (Michael S)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Tue, 27 Feb 2024 14:59:50 +0200
Organization: A noiseless patient Spider
Lines: 151
Message-ID: <20240227145950.00001d67@yahoo.com>
References: <urie91$2km5r$1@dont-email.me>
<urkimd$36dg1$1@dont-email.me>
<urkl1d$36vet$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Injection-Info: dont-email.me; posting-host="c2b98580797b676cb445ea20bddff5b8";
logging-data="3296998"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pShXnJGbal0CT8re6A4YaeA7Fxdl0hUE="
Cancel-Lock: sha1:52JAvWUC90iR2dv+LcZtKux6A/g=
X-Newsreader: Claws Mail 4.1.1 (GTK 3.24.34; x86_64-w64-mingw32)
 by: Michael S - Tue, 27 Feb 2024 12:59 UTC

On Tue, 27 Feb 2024 13:37:00 +0100
David Brown <david.brown@hesbynett.no> wrote:

> On 27/02/2024 12:57, Thiago Adams wrote:
> > On 26/02/2024 13:29, bart wrote:
> >> One weak part of my C compiler is in initialisation:
> >>
> >> * No runtime data is allowed for {...} data inside functions
> >>
> >> * For nested structs and arrays, the pattern of {...} braces must
> >> exactly match the type structure, no missing braces allowed. (So
> >> {0} can't be used to zero a struct; only missing elements in
> >> arrays are allowed)
> >>
> >> * No support for designated initialisers
> >>
> >> * No support for compound literals (this is all part of the same
> >> aspect)
> >>
> >> So I thought I'd bite the bullet and fix all this. Until I came
> >> across section 6.7.9 (in my C99 draft standard, which is
> >> Initialisation).
> >>
> >> Paragraphs p17 to p38 described the rules for braces and for
> >> designated initialisers. I vaguely remember that it was
> >> complicated, I didn't realise it was this complicated!
> >>
> >> For example, p35 shows this example:
> >>
> >>      struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2};
> >>
> >> I couldn't grok it. I had to use this loop to find out what was
> >> going on:
> >>
> >>    for (int i=0; i<sizeof(w)/sizeof(w[0]); ++i) {
> >>        printf("(%d %d %d) %d\n", w[i].a[0], w[i].a[1], w[i].a[2],
> >> w[i].b);
> >>    }
> >>
> >> What this means is that you can declare a data strucure of any
> >> complexity, and initialise random, nested elements within a single
> >> set of { ... } braces. Here's a simple example:
> >>
> >>      int A[][10] = {
> >>          [7][3] = 99
> >>      };
> >>
> >> I didn't know you could combine designators like this (I've never
> >> seen such examples), but I don't believe that is necessary. This
> >> example could also be written like this:
> >>
> >>      int A[][10] = {
> >>          [7] = {[3] = 99}
> >>      };
> >>
> >> This doesn't require combining designators, and here there is a
> >> better match in the structure of the braces: '99' is inside 2 sets
> >> of braces rather than 1. But there is a hidden danger here too:
> >>
> >>      int A[] = {
> >>         [someexpr] = 99
> >>      }
> >>
> >> For a local array, 'someexpr' could be 20, or it could be 2000000,
> >> high enough to cause stack overflow if you're lucky. Or it could
> >> just silently create a far bigger array than expected.
> >>
> >> Here's another example from p36:
> >>
> >>      int a[MAX] = {1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0};
> >>
> >> p37 explains what it means, but not well. I think that the '6 4 2
> >> 0' values follow element a[MAX-5]. It also suggests that is it
> >> possible to define values multiple times:
> >>
> >>      int A[] = {[3]=1, [3]=2, [3]=3};
> >>
> >> It can get even more hairy:
> >>
> >>      int A[] = {[3]=1, [3]=2, [3]=A[3]};
> >>
> >> What is the value of A[3]? My guess was 2, but when I tried it, it
> >> was 0 with gcc, 2 with tcc, an error with DMC, and -108754328 with
> >> clang.
> >>
> >> Presumably this is because it's using an uninitialised value of
> >> A[3]? But A[3] IS initialised, at least twice!
> >>
> >>
> >> This is all far more chaotic than I'd expected, and I decided not
> >> to bother upgrading my compiler. I have trouble implementing
> >> something that (1) I don't agree with because it's been poorly
> >> devised;(2) I don't understand anyway.
> >>
> >
> > I also want to understand better this.
> > I think the design must have some logic, I never stop to understand
> > this. I think the best way is see some generated code, because it
> > is maybe related with initialization performance. For instance,
> > fill zeros first then do the other initialization.
> >
>
> The order of initialisation is by the order of the initialisers
> (subject, as always, to the "as if" rule). So if you write :
>
> int A[] = {[3]=1, [3]=2, [3]=3};
>
> then the final "[3] = 3" is applied last, and overrides the others.
> This does not seem very useful to me, as it stands. However, like
> many features of C99, this has come about as a standardisation of gcc
> extensions, and that is how the gcc syntax worked - therefore, C99
> copied it for consistency with existing code. In gcc, it makes much
> more sense because you can write :
>
> int A[] = { [0 .. 9] = 1, [10 .. 29] = 2, [5] = 17 };
>
> and use this feature to override a value given in a range.
>
> <https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html>
>
>
> The order of evaluation of the initialisers, by contrast, is
> unspecified. It is also unspecified if overridden initialisers are
> evaluated (for their side-effects) at all. Again, this is consistent
> with the gcc extension, and it gives compilers more freedom in how
> they organise the code. That does mean that code like this:
>
> int A[] = {[3]=1, [3]=2, [3]=A[3]};
>
> does not have a well-defined and consistent result. But then,
> neither does "int x = x;", even though it is valid code. (It is
> sometimes used as an idiom to tell static error checkers and other
> programmers that you know "x" is not initialised, and that there
> should be no warning even if it appears to be read before
> initialisation.)
>
>
>

Designated initialization of auto objects is small and unimportant
convenience feature.
The big convenience gain comes when designated initializers applied
to static and global objects. And in that case most of the problems
mentioned above simply do not exist.

If I were in bart's place, authoring compiler that does not try to be
fully standard, I'd omit designated initialization of auto objects
altogether.

Re: Data Initialisation

<urks06$38fsn$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Tue, 27 Feb 2024 15:35:50 +0100
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <urks06$38fsn$1@dont-email.me>
References: <urie91$2km5r$1@dont-email.me> <urkimd$36dg1$1@dont-email.me>
<urkl1d$36vet$1@dont-email.me> <20240227145950.00001d67@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 27 Feb 2024 14:35:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5b013b32b0ae31855296b19afd591ff2";
logging-data="3424151"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19a3FzxtPiChw+SSIG4YVyDBLPhFb8jbDs="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:hObzEcsp23JVkctOYGn4Us5sw3A=
Content-Language: en-GB
In-Reply-To: <20240227145950.00001d67@yahoo.com>
 by: David Brown - Tue, 27 Feb 2024 14:35 UTC

On 27/02/2024 13:59, Michael S wrote:

> Designated initialization of auto objects is small and unimportant
> convenience feature.
> The big convenience gain comes when designated initializers applied
> to static and global objects. And in that case most of the problems
> mentioned above simply do not exist.
>
> If I were in bart's place, authoring compiler that does not try to be
> fully standard, I'd omit designated initialization of auto objects
> altogether.
>

I would agree that that is a reasonable half-way step. It might be
easier for him, but only Bart can answer that. It would certainly mean
that some of the things he sees as complications no longer apply - all
initialisers must be constants, so there are no questions about
evaluation order. But he would still have to deal with overrides in
initialiser lists, and what he described as "long chains of designators".

Re: Data Initialisation

<urllv6$3eaa8$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (bart)
Newsgroups: comp.lang.c
Subject: Re: Data Initialisation
Date: Tue, 27 Feb 2024 21:59:02 +0000
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <urllv6$3eaa8$1@dont-email.me>
References: <urie91$2km5r$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 27 Feb 2024 21:59:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="20b1b8f346f60418f3ea58d335a2a8b5";
logging-data="3615048"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Z/lxY76FZgoH6urmEmQocH0Ucanv3+G8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:HEbJ4Y1a6KORd7/UjpJs38BgGVo=
Content-Language: en-GB
In-Reply-To: <urie91$2km5r$1@dont-email.me>
 by: bart - Tue, 27 Feb 2024 21:59 UTC

On 26/02/2024 16:29, bart wrote:
> One weak part of my C compiler is in initialisation:
....
> This is all far more chaotic than I'd expected, and I decided not to
> bother upgrading my compiler. I have trouble implementing something that
> (1) I don't agree with because it's been poorly devised;(2) I don't
> understand anyway.

I'm going to look at rewriting the front end of the compiler. This will
add an extra pass, separating parsing + name resolution from type analysis.

At the moment all that is done as one pass which gives little
flexibility. I should be able to at least parse that data-init code and
worry about what it means later. However member names can't be resolved
until the second pass as they depend on what type things are.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor