Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

On the Internet, nobody knows you're a dog. -- Cartoon caption


devel / comp.lang.c / Re: Padding between char array/VLA in struct?

SubjectAuthor
* Padding between char array/VLA in struct?Ian Pilcher
+* Re: Padding between char array/VLA in struct?James Kuyper
|`* Re: Padding between char array/VLA in struct?Keith Thompson
| `- Re: Padding between char array/VLA in struct?James Kuyper
+* Re: Padding between char array/VLA in struct?Bart
|`* Re: Padding between char array/VLA in struct?Keith Thompson
| `* Re: Padding between char array/VLA in struct?Bart
|  +* Re: Padding between char array/VLA in struct?Andrey Tarasevich
|  |+- Re: Padding between char array/VLA in struct?Andrey Tarasevich
|  |`* Re: Padding between char array/VLA in struct?Keith Thompson
|  | `* Re: Padding between char array/VLA in struct?Andrey Tarasevich
|  |  `- Re: Padding between char array/VLA in struct?David Brown
|  +- Re: Padding between char array/VLA in struct?Keith Thompson
|  `* Re: Padding between char array/VLA in struct?BGB
|   `* Re: Padding between char array/VLA in struct?Andrey Tarasevich
|    `* Re: Padding between char array/VLA in struct?BGB
|     `- Re: Padding between char array/VLA in struct?BGB
+* Re: Padding between char array/VLA in struct?Keith Thompson
|`* Re: Padding between char array/VLA in struct?Bart
| `- Re: Padding between char array/VLA in struct?Keith Thompson
+- Re: Padding between char array/VLA in struct?Andrey Tarasevich
`* Re: Padding between char array/VLA in struct?Ian Pilcher
 +* Re: Padding between char array/VLA in struct?Tim Rentsch
 |`* Re: Padding between char array/VLA in struct?Ian Pilcher
 | +- Re: Padding between char array/VLA in struct?Keith Thompson
 | `- Re: Padding between char array/VLA in struct?antispam
 `* Re: Padding between char array/VLA in struct?Keith Thompson
  `* Re: Padding between char array/VLA in struct?Manfred
   `- Re: Padding between char array/VLA in struct?Keith Thompson

Pages:12
Padding between char array/VLA in struct?

<se9b5g$l2n$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: arequip...@gmail.com (Ian Pilcher)
Newsgroups: comp.lang.c
Subject: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 12:48:31 -0500
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <se9b5g$l2n$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 17:48:32 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="54925c9e09c0a259650401d0bbf9d4ea";
logging-data="21591"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RxU11urm9nisSZMqRoFonSarDqt2KAS4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:hrpdkLPA1tdlNxDFVD0O213oITQ=
Content-Language: en-US
X-Mozilla-News-Host: news://news.eternal-september.org:119
 by: Ian Pilcher - Mon, 2 Aug 2021 17:48 UTC

Given the following:

struct foo {
/* other members */
char prefix[PREFIX_SIZE];
char name[];
};

Is there any circumstance in which the compiler is permitted to insert
padding between the 'prefix' and 'name' members?

I'm 99% sure than the answer is no, but I've been unable to track down
the actual rules anywhere.

--
========================================================================
Ian Pilcher arequipeno@gmail.com
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================

Re: Padding between char array/VLA in struct?

<se9c92$t69$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 14:07:29 -0400
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <se9c92$t69$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 18:07:30 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="49a4224d46ae5361a4ceba03b8dc2fcb";
logging-data="29897"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UA3cJK/yMeR2gSkRii9tyeuN+wmmips8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:7ZXfvAVLoYuwcKtx18FgcvbclPk=
In-Reply-To: <se9b5g$l2n$1@dont-email.me>
Content-Language: en-US
 by: James Kuyper - Mon, 2 Aug 2021 18:07 UTC

On 8/2/21 1:48 PM, Ian Pilcher wrote:
> Given the following:
>
> struct foo {
> /* other members */

Note: this is a VLA unless PREFIX_SIZE is a macro that expands to an
integer constant expression.

> char prefix[PREFIX_SIZE];

This is NOT a VLA - it is a flexible array member:

> char name[];
> };
>
> Is there any circumstance in which the compiler is permitted to insert
> padding between the 'prefix' and 'name' members?
>
> I'm 99% sure than the answer is no, but I've been unable to track down
> the actual rules anywhere.

"There may be unnamed padding within a structure object, but not at its
beginning." (6.7.2.1p15).

Thus, the only restriction on such padding is that it can't be at the
beginning. I know of no other restrictions on the presence, the size, or
the location of any such padding.

There are some relevant special rules covering flexible array members,
but they don't revoke the permission give by 6.7.2.1p15 to have such
padding:

"... when a . (or ->) operator has a left operand that is
(a pointer to) a structure with a flexible array member and the right
operand names that member, it behaves as if that member were replaced
with the longest array (with the same element type) that would not make
the structure larger than the object being accessed; the offset of the
array shall remain that of the flexible array member, even if this would
differ from that of the replacement array." (6.7.2.1p18)

Since the replacement array would be allowed to have padding that
separates it from the prefix array, then that is also permitted for the
flexible array member.

Re: Padding between char array/VLA in struct?

<se9cfv$uta$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 19:11:06 +0100
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <se9cfv$uta$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 Aug 2021 18:11:11 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6fd5bf5f89068ed62160f01a728fb45d";
logging-data="31658"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KyZltWClV9Df/r9V5czNqjxY0aQjG8i8="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:8VQaF2LzlzZuHes/LQt4FoFkMhk=
In-Reply-To: <se9b5g$l2n$1@dont-email.me>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210802-10, 2/8/2021), Outbound message
 by: Bart - Mon, 2 Aug 2021 18:11 UTC

On 02/08/2021 18:48, Ian Pilcher wrote:
> Given the following:
>
>   struct foo {
>       /* other members */
>       char prefix[PREFIX_SIZE];
>       char name[];
>   };
>
> Is there any circumstance in which the compiler is permitted to insert
> padding between the 'prefix' and 'name' members?
>
> I'm 99% sure than the answer is no, but I've been unable to track down
> the actual rules anywhere.
>

I think only for alignment purposes. And there is no need when name has
char[] type. With int name[], then possibly.

BTW there is no VLA here unless PREFIX_SIZE is not a compile-time
expression (and I'm not sure even then).

Re: Padding between char array/VLA in struct?

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

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 11:12:39 -0700
Organization: None to speak of
Lines: 25
Message-ID: <87eebbaf6w.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="26747"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/exGKY6NBECiJ5QFHjN1sG"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:sMqPit07FTa56XnYwMIbIxP/+EY=
sha1:iniS76e1Y72k97K4d2UdoCUVFGE=
 by: Keith Thompson - Mon, 2 Aug 2021 18:12 UTC

Ian Pilcher <arequipeno@gmail.com> writes:
> Given the following:
>
> struct foo {
> /* other members */
> char prefix[PREFIX_SIZE];
> char name[];
> };
>
> Is there any circumstance in which the compiler is permitted to insert
> padding between the 'prefix' and 'name' members?
>
> I'm 99% sure than the answer is no, but I've been unable to track down
> the actual rules anywhere.

I'm 99% sure the answer is "always".

Compilers typically insert padding only when it's necessary to meet
alignment requirements, but compilers are allowed to insert arbitrary
padding between members.

--
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: Padding between char array/VLA in struct?

<se9d6b$4d0$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 19:23:02 +0100
Organization: A noiseless patient Spider
Lines: 35
Message-ID: <se9d6b$4d0$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me>
<87eebbaf6w.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 18:23:07 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6fd5bf5f89068ed62160f01a728fb45d";
logging-data="4512"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX190ggeiumgrrWdnVkNKAYgS4h3JL/6Ia5o="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:G/FeMvDakfoPffVRKNZr4OvfdtQ=
In-Reply-To: <87eebbaf6w.fsf@nosuchdomain.example.com>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210802-10, 2/8/2021), Outbound message
 by: Bart - Mon, 2 Aug 2021 18:23 UTC

On 02/08/2021 19:12, Keith Thompson wrote:
> Ian Pilcher <arequipeno@gmail.com> writes:
>> Given the following:
>>
>> struct foo {
>> /* other members */
>> char prefix[PREFIX_SIZE];
>> char name[];
>> };
>>
>> Is there any circumstance in which the compiler is permitted to insert
>> padding between the 'prefix' and 'name' members?
>>
>> I'm 99% sure than the answer is no, but I've been unable to track down
>> the actual rules anywhere.
>
> I'm 99% sure the answer is "always".
>
> Compilers typically insert padding only when it's necessary to meet
> alignment requirements, but compilers are allowed to insert arbitrary
> padding between members.
>

But not for no reason, surely? And not when #pragma pack(1) is in effect.

And whatever padding might be inserted, the same rules must be in effect
across all compilers for that platform, otherwise functions in modules
and in external libraries created with mixed compilers can't call each
other.

Since I am familiar with duplicating with layout of C structs from
outside the language, to the extent of also duplicating the algorithms
for alignment and padding, this would be an impossible task if
effectively random padding could be inserted.

Re: Padding between char array/VLA in struct?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 11:25:46 -0700
Organization: None to speak of
Lines: 20
Message-ID: <87a6lzael1.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me> <se9c92$t69$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="26747"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RsK2TTuHAUw6Ib34nFUEg"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:7DItyLK+YxptxMSPDrG7zjuH8Hs=
sha1:mkrmLfCGngsJT0uwzwflqJnePfg=
 by: Keith Thompson - Mon, 2 Aug 2021 18:25 UTC

James Kuyper <jameskuyper@alumni.caltech.edu> writes:
> On 8/2/21 1:48 PM, Ian Pilcher wrote:
>> Given the following:
>>
>> struct foo {
>> /* other members */
>
> Note: this is a VLA unless PREFIX_SIZE is a macro that expands to an
> integer constant expression.
>
>> char prefix[PREFIX_SIZE];

This cannot be a VLA because VLAs are not allowed as struct or union members.

[...]

--
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: Padding between char array/VLA in struct?

<875ywnaehw.fsf@nosuchdomain.example.com>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 11:27:39 -0700
Organization: None to speak of
Lines: 35
Message-ID: <875ywnaehw.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="26747"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ltkSDOto+PYSmrSoS2px+"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:ohs8mZ9kjxa2D/ZtMJvOu3uB388=
sha1:FeVTMfeZ55kzNQlKQcMHwxF41tk=
 by: Keith Thompson - Mon, 2 Aug 2021 18:27 UTC

Bart <bc@freeuk.com> writes:
> On 02/08/2021 18:48, Ian Pilcher wrote:
>> Given the following:
>>   struct foo {
>>       /* other members */
>>       char prefix[PREFIX_SIZE];
>>       char name[];
>>   };
>> Is there any circumstance in which the compiler is permitted to
>> insert
>> padding between the 'prefix' and 'name' members?
>> I'm 99% sure than the answer is no, but I've been unable to track
>> down
>> the actual rules anywhere.
>
> I think only for alignment purposes. And there is no need when name
> has char[] type. With int name[], then possibly.

Compilers are unlikely to insert padding other than for alignment
purposes, but the standard doesn't make that restriction. A conforming
compiler can add as much padding as it likes between members or after
the last member (if the last member isn't a flexible array member, which
it is in this case).

> BTW there is no VLA here unless PREFIX_SIZE is not a compile-time
> expression (and I'm not sure even then).

If PREFIX_SIZE is not a compile time expression, it's a constraint
violation. Struct and union member cannot be VLAs. (Also, VLA support
is optional as of C11.)

--
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: Padding between char array/VLA in struct?

<se9dnc$8a3$1@dont-email.me>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 11:32:10 -0700
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <se9dnc$8a3$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 Aug 2021 18:32:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e74d1750d80e9039aee0b76189e95916";
logging-data="8515"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Mr5pFx3HHJrO+3YjLvKDn"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:D27LJzhb7HxVubD8PrZa4wucZvM=
In-Reply-To: <se9b5g$l2n$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Mon, 2 Aug 2021 18:32 UTC

On 8/2/2021 10:48 AM, Ian Pilcher wrote:
> Given the following:
>
>   struct foo {
>       /* other members */
>       char prefix[PREFIX_SIZE];
>       char name[];
>   };
>
> Is there any circumstance in which the compiler is permitted to insert
> padding between the 'prefix' and 'name' members?
>
> I'm 99% sure than the answer is no, but I've been unable to track down
> the actual rules anywhere.
>

As far as the formal rules of the language are considered, the compiler
is _always_ permitted to insert padding between struct members (and
after the last member) for whatever reason it pleases. Extra padding can
always be added anywhere, except before the first member of the struct.

So, the answer is "yes".

Also, it is not clear why you mention VLA in the title of your question.
The language does not allow VLA as a struct members. The last member of
your struct is called "flexible array member". This is not VLA and has
nothing to do with VLA.

--
Best regards,
Andrey Tarasevich

Re: Padding between char array/VLA in struct?

<se9fa1$ikv$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: arequip...@gmail.com (Ian Pilcher)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 13:59:13 -0500
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <se9fa1$ikv$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 Aug 2021 18:59:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="54925c9e09c0a259650401d0bbf9d4ea";
logging-data="19103"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZNRChgvC+hfusYATROw9X5qlUzmVk7eY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:eB5w5GwU+zRXdjkmi5ofYV06Lhs=
In-Reply-To: <se9b5g$l2n$1@dont-email.me>
Content-Language: en-US
 by: Ian Pilcher - Mon, 2 Aug 2021 18:59 UTC

On 8/2/21 12:48 PM, Ian Pilcher wrote:
> Given the following:
>
>   struct foo {
>       /* other members */
>       char prefix[PREFIX_SIZE];
>       char name[];
>   };

As has been pointed out, the 'name' member is a flexible array member,
not a variable length array. I apologize for the mistake.

Also, PREFIX_SIZE is intended to represent a macro that expands to a
positive(!) integer constant expression.

The consensus seems to be that no one can think of a good reason that a
compiler would insert padding between the 'prefix' and 'name' members,
but there's nothing in any of the language standards that would forbid
it from doing so. That would certainly explain my failure to find any
such rule.

(_Static_assert here I come!)

Thanks!

--
========================================================================
Ian Pilcher arequipeno@gmail.com
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================

Re: Padding between char array/VLA in struct?

<se9htr$5ll$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 20:43:50 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <se9htr$5ll$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 19:43:55 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="6fd5bf5f89068ed62160f01a728fb45d";
logging-data="5813"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18sgQY1aDq8VVOJin3QLWteodQax8DzoEY="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:Z5wMt5NWSxBKEUl2cEBBfYIR1Uk=
In-Reply-To: <875ywnaehw.fsf@nosuchdomain.example.com>
X-Antivirus-Status: Clean
Content-Language: en-GB
X-Antivirus: AVG (VPS 210802-10, 2/8/2021), Outbound message
 by: Bart - Mon, 2 Aug 2021 19:43 UTC

On 02/08/2021 19:27, Keith Thompson wrote:
> Bart <bc@freeuk.com> writes:

>> BTW there is no VLA here unless PREFIX_SIZE is not a compile-time
>> expression (and I'm not sure even then).
>
> If PREFIX_SIZE is not a compile time expression, it's a constraint
> violation. Struct and union member cannot be VLAs. (Also, VLA support
> is optional as of C11.)
>

This program works on gcc, tcc and lccwin:

#include <stdio.h>
int main(void) {
int n=100;
typedef struct{int xx[n];} S;

S a;

printf("sizeof(S) = %d\n",(int)sizeof(S));
}

The output is respectively 400, 8 and 32.

It fails with DMC, clang, msvc and bcc (but the last two don't support
VLAs anyway).

The mind boggles as to how it's implemented, for example with static,
auto and heap-allocated arrays of S, or with linked lists using S nodes,
or when S itself contains structs with such VLAs.

Re: Padding between char array/VLA in struct?

<867dh34oia.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 12:47:57 -0700
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <867dh34oia.fsf@linuxsc.com>
References: <se9b5g$l2n$1@dont-email.me> <se9fa1$ikv$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="060020ed4df747d792a42c4a015e636f";
logging-data="3226"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LQkzx8C3sEmFsKgqvIBajeRXbZgfY+j8="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:RdLY5P4YBDr9gyG3ocB0h+qM440=
sha1:9vGjIW0Qbq6AIX/pfSAwWYH8exU=
 by: Tim Rentsch - Mon, 2 Aug 2021 19:47 UTC

Ian Pilcher <arequipeno@gmail.com> writes:

> On 8/2/21 12:48 PM, Ian Pilcher wrote:
>
>> Given the following:
>>
>> struct foo {
>> /* other members */
>> char prefix[PREFIX_SIZE];
>> char name[];
>> };
>
> As has been pointed out, the 'name' member is a flexible array member,
> not a variable length array. I apologize for the mistake.
>
> Also, PREFIX_SIZE is intended to represent a macro that expands to a
> positive(!) integer constant expression.
>
> The consensus seems to be that no one can think of a good reason that a
> compiler would insert padding between the 'prefix' and 'name' members,
> but there's nothing in any of the language standards that would forbid
> it from doing so. That would certainly explain my failure to find any
> such rule.
>
> (_Static_assert here I come!)

I am curious to know why you care. Would having a guarantee that
there is no padding affect what code you would write? If it
would then there is a good chance that guarantee-depending code
has undefined behavior somewhere.

Re: Padding between char array/VLA in struct?

<se9jh0$gre$1@dont-email.me>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 13:11:12 -0700
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <se9jh0$gre$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 2 Aug 2021 20:11:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e74d1750d80e9039aee0b76189e95916";
logging-data="17262"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+2jgligfZFxtxPgEETA5Vu"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:qqMefhwnAOP9OuitV1n+2iU0kQs=
In-Reply-To: <se9htr$5ll$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Mon, 2 Aug 2021 20:11 UTC

On 8/2/2021 12:43 PM, Bart wrote:
>>
>
> This program works on gcc, tcc and lccwin:
>
> #include <stdio.h>
> int main(void) {
>     int n=100;
>     typedef struct{int xx[n];} S;
>
>     S a;
>
>     printf("sizeof(S) = %d\n",(int)sizeof(S));
> }
>
> The output is respectively 400, 8 and 32.
>
> It fails with DMC, clang, msvc and bcc (but the last two don't support
> VLAs anyway).

It will fail in GCC as well if you run it in `-pedantic` mode.

> The mind boggles as to how it's implemented, for example with static,
> auto and heap-allocated arrays of S, or with linked lists using S nodes,
> or when S itself contains structs with such VLAs.

There's nothing mind-boggling about how it is implemented once you
realize that this is allowed for trailing array member only. This GCC
extension is a hybrid of "flexible array member" memory layout and VLA
semantics.

--
Best regards,
Andrey Tarasevich

Re: Padding between char array/VLA in struct?

<871r7ba9hq.fsf@nosuchdomain.example.com>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 13:15:45 -0700
Organization: None to speak of
Lines: 56
Message-ID: <871r7ba9hq.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me>
<87eebbaf6w.fsf@nosuchdomain.example.com> <se9d6b$4d0$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="7303"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191Y836FDPrnD4stSAD5u4O"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:st3Vxqz66kkOnTeKKK//IzDdpEs=
sha1:1yA0ENJwkAcWwnS4+MV5I9lH1F4=
 by: Keith Thompson - Mon, 2 Aug 2021 20:15 UTC

Bart <bc@freeuk.com> writes:
> On 02/08/2021 19:12, Keith Thompson wrote:
>> Ian Pilcher <arequipeno@gmail.com> writes:
>>> Given the following:
>>>
>>> struct foo {
>>> /* other members */
>>> char prefix[PREFIX_SIZE];
>>> char name[];
>>> };
>>>
>>> Is there any circumstance in which the compiler is permitted to insert
>>> padding between the 'prefix' and 'name' members?
>>>
>>> I'm 99% sure than the answer is no, but I've been unable to track down
>>> the actual rules anywhere.
>> I'm 99% sure the answer is "always".
>> Compilers typically insert padding only when it's necessary to meet
>> alignment requirements, but compilers are allowed to insert arbitrary
>> padding between members.
>
> But not for no reason, surely? And not when #pragma pack(1) is in effect.

Yes, for no reason.

To be clear, I was talking about what the C standard permits.

The standard says that padding may be inserted between members. It says
nothing about what reasons a compiler must have for doing so. A
compiler that inserts unneeded padding "for no reason" would likely be a
poor compiler, but it would not violate the standard.

#pragma pack is non-standard.

> And whatever padding might be inserted, the same rules must be in
> effect across all compilers for that platform, otherwise functions in
> modules and in external libraries created with mixed compilers can't
> call each other.

Requirements on alignment and structure layout are typically imposed by
ABIs, not by the C standard. The standard has some requirements for
struct layout (you can read the relevant section yourself if you're
interested), but those requirements do not include any restrictions on
the size of padding.

> Since I am familiar with duplicating with layout of C structs from
> outside the language, to the extent of also duplicating the algorithms
> for alignment and padding, this would be an impossible task if
> effectively random padding could be inserted.

ABIs.

--
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: Padding between char array/VLA in struct?

<se9jui$k2u$1@dont-email.me>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 13:18:24 -0700
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <se9jui$k2u$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
<se9jh0$gre$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 20:18:26 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e74d1750d80e9039aee0b76189e95916";
logging-data="20574"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19i8LtGBWR7AjydK3ujYc33"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:0cetdXS3UL+Zi0dYrJKE32xtrBM=
In-Reply-To: <se9jh0$gre$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Mon, 2 Aug 2021 20:18 UTC

On 8/2/2021 1:11 PM, Andrey Tarasevich wrote:
>
> There's nothing mind-boggling about how it is implemented once you
> realize that this is allowed for trailing array member only. This GCC
> extension is a hybrid of "flexible array member" memory layout and VLA
> semantics.
>

An interesting experiment that immediately comes to mind

int n=100;
typedef struct{ int xx[n]; } S;

S a;
int i = 0;

printf("%zu\n", sizeof(*(++i, &a.xx)));
printf("%d\n", i);

printf("%zu\n", sizeof(*(++i, &a)));
printf("%d\n", i);

outputs

400
1
400
1

which indicates that GCC perceives `a.xx` as an entity having variably
modified type (a VLA), but doesn't dare to go as far as treating the
struct type itself as a variably modified type.

--
Best regards,
Andrey Tarasevich

Re: Padding between char array/VLA in struct?

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 13:19:48 -0700
Organization: None to speak of
Lines: 45
Message-ID: <87wnp38uqj.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="7303"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19b1AMM7AQ1Djd52laW/dgn"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:LrJa5hMC27nlsATuf9DRR+ZXA8s=
sha1:4bbfMey4P4LH/igqzwZ88lJzqoY=
 by: Keith Thompson - Mon, 2 Aug 2021 20:19 UTC

Bart <bc@freeuk.com> writes:
> On 02/08/2021 19:27, Keith Thompson wrote:
>> Bart <bc@freeuk.com> writes:
>
>>> BTW there is no VLA here unless PREFIX_SIZE is not a compile-time
>>> expression (and I'm not sure even then).
>> If PREFIX_SIZE is not a compile time expression, it's a constraint
>> violation. Struct and union member cannot be VLAs. (Also, VLA support
>> is optional as of C11.)
>
> This program works on gcc, tcc and lccwin:
>
> #include <stdio.h>
> int main(void) {
> int n=100;
> typedef struct{int xx[n];} S;
>
> S a;
>
> printf("sizeof(S) = %d\n",(int)sizeof(S));
> }
>
> The output is respectively 400, 8 and 32.
>
> It fails with DMC, clang, msvc and bcc (but the last two don't support
> VLAs anyway).

To be a bit clearer, defining a struct member as a VLA is a constraint
violation. A conforming C compiler must diagnose it. It can then
proceed to generate code for it -- or a non-conforming compiler (such as
gcc by default) can do anything it likes.

> The mind boggles as to how it's implemented, for example with static,
> auto and heap-allocated arrays of S, or with linked lists using S
> nodes, or when S itself contains structs with such VLAs.

gcc's extension does not include support for static structs with VLA
members. The other cases don't strike me as particularly difficult,
though of course they require a bit more work than for structs whose
size is known at compile time.

--
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: Padding between char array/VLA in struct?

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

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 13:22:16 -0700
Organization: None to speak of
Lines: 33
Message-ID: <87sfzr8umf.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me> <se9fa1$ikv$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="7303"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Yn673eAb6MLvCjTasQ8iG"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:GjK3h7N9lr+eZZr6eCSTpQM+mEg=
sha1:DN9joAygftllZIuz3oPxz8pr59M=
 by: Keith Thompson - Mon, 2 Aug 2021 20:22 UTC

Ian Pilcher <arequipeno@gmail.com> writes:
> On 8/2/21 12:48 PM, Ian Pilcher wrote:
>> Given the following:
>>   struct foo {
>>       /* other members */
>>       char prefix[PREFIX_SIZE];
>>       char name[];
>>   };
>
> As has been pointed out, the 'name' member is a flexible array member,
> not a variable length array. I apologize for the mistake.
>
> Also, PREFIX_SIZE is intended to represent a macro that expands to a
> positive(!) integer constant expression.
>
> The consensus seems to be that no one can think of a good reason that a
> compiler would insert padding between the 'prefix' and 'name' members,
> but there's nothing in any of the language standards that would forbid
> it from doing so. That would certainly explain my failure to find any
> such rule.
>
> (_Static_assert here I come!)

The ABI for whatever target platform you're interested in might have
something to say about it.

(I can imagine that some compiler might find it convenient to give the
name[] member an alignment stricter than 1 byte.)

--
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: Padding between char array/VLA in struct?

<se9kev$ndj$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: arequip...@gmail.com (Ian Pilcher)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 15:27:10 -0500
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <se9kev$ndj$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9fa1$ikv$1@dont-email.me>
<867dh34oia.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 20:27:11 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="54925c9e09c0a259650401d0bbf9d4ea";
logging-data="23987"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UfQTkT1TcRF2dcx8KT8VsjNN6D0dkCpY="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:OH2TIGIPepCt8sSZWACGYW3TcRQ=
In-Reply-To: <867dh34oia.fsf@linuxsc.com>
Content-Language: en-US
 by: Ian Pilcher - Mon, 2 Aug 2021 20:27 UTC

On 8/2/21 2:47 PM, Tim Rentsch wrote:
> I am curious to know why you care. Would having a guarantee that
> there is no padding affect what code you would write? If it
> would then there is a good chance that guarantee-depending code
> has undefined behavior somewhere.

It's just a convenient way to refer to either the "full" name (including
the prefix) or the unique part of the name. For example, I've got a
bunch of struct foo's, each of which has a name that starts with "FOO:",
and most of the time I don't care about the prefix.

#define FOO_PREFIX "FOO:"

static const char foo_prefix[sizeof FOO_PREFIX - 1] = FOO_PREFIX;

struct foo {
/* other members */
char prefix[sizeof foo_prefix];
char name[];
}

struct foo *alloc_foo(char *name)
{
struct foo *f;

f = malloc(sizeof *f + strlen(name) + 1);

// Or do something clever with sprintf()
memcpy(&f->prefix, foo_prefix, sizeof foo_prefix);
memcpy(&f->name, name, strlen(name) + 1);

return f;
}

For a given struct foo *f, I can access the full name as f->prefix, and
I can access the unique part as f->name.

--
========================================================================
Ian Pilcher arequipeno@gmail.com
-------- "I grew up before Mark Zuckerberg invented friendship" --------
========================================================================

Re: Padding between char array/VLA in struct?

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

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 13:31:00 -0700
Organization: None to speak of
Lines: 57
Message-ID: <87o8af8u7v.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
<se9jh0$gre$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="7303"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ShUElGwZEfz+DIHoj/Zxn"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:eeH3njoXh4lKF7MsTrkqrWeCras=
sha1:7aoz5ZTJ2bl6x5W0BXEf6T/DIDA=
 by: Keith Thompson - Mon, 2 Aug 2021 20:31 UTC

Andrey Tarasevich <andreytarasevich@hotmail.com> writes:
> On 8/2/2021 12:43 PM, Bart wrote:
>>>
>> This program works on gcc, tcc and lccwin:
>> #include <stdio.h>
>> int main(void) {
>>     int n=100;
>>     typedef struct{int xx[n];} S;
>>     S a;
>>     printf("sizeof(S) = %d\n",(int)sizeof(S));
>> }
>> The output is respectively 400, 8 and 32.
>> It fails with DMC, clang, msvc and bcc (but the last two don't
>> support VLAs anyway).
>
> It will fail in GCC as well if you run it in `-pedantic` mode.
>
>> The mind boggles as to how it's implemented, for example with
>> static, auto and heap-allocated arrays of S, or with linked lists
>> using S nodes, or when S itself contains structs with such VLAs.
>
> There's nothing mind-boggling about how it is implemented once you
> realize that this is allowed for trailing array member only. This GCC
> extension is a hybrid of "flexible array member" memory layout and VLA
> semantics.

No, gcc's extension appears to allow VLAs as struct members. This
compiles and runs without error and, as expected, prints different
values when it's executed again after more than a second:

#include <stdio.h>
#include <time.h>
#include <stddef.h>
int main(void) {
const int size = time(NULL) % 10 + 10;
struct s {
char s0[size];
char s1[size];
char s2[size];
};

struct s obj;

printf("sizeof obj = %zu\n", sizeof obj);
printf("%zu %zu %zu\n",
offsetof(struct s, s0),
offsetof(struct s, s1),
offsetof(struct s, s2));
}

(I didn't find this extension in gcc's documentation, but I didn't look
very hard.)

--
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: Padding between char array/VLA in struct?

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

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 02 Aug 2021 13:36:06 -0700
Organization: None to speak of
Lines: 49
Message-ID: <87k0l38tzd.fsf@nosuchdomain.example.com>
References: <se9b5g$l2n$1@dont-email.me> <se9fa1$ikv$1@dont-email.me>
<867dh34oia.fsf@linuxsc.com> <se9kev$ndj$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="6d572ab74fff7e435a45dc17f7728aa8";
logging-data="7303"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ASrlEilAlRIqPBUc1Plp/"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:TA8/NhaIhH3V3OwWp7vrWFISGik=
sha1:ZpAX9SrIvmt1Fgdqxdagc17d0FM=
 by: Keith Thompson - Mon, 2 Aug 2021 20:36 UTC

Ian Pilcher <arequipeno@gmail.com> writes:
> On 8/2/21 2:47 PM, Tim Rentsch wrote:
>> I am curious to know why you care. Would having a guarantee that
>> there is no padding affect what code you would write? If it
>> would then there is a good chance that guarantee-depending code
>> has undefined behavior somewhere.
>
> It's just a convenient way to refer to either the "full" name (including
> the prefix) or the unique part of the name. For example, I've got a
> bunch of struct foo's, each of which has a name that starts with "FOO:",
> and most of the time I don't care about the prefix.
>
> #define FOO_PREFIX "FOO:"
>
> static const char foo_prefix[sizeof FOO_PREFIX - 1] = FOO_PREFIX;
>
> struct foo {
> /* other members */
> char prefix[sizeof foo_prefix];
> char name[];
> }
>
> struct foo *alloc_foo(char *name)
> {
> struct foo *f;
>
> f = malloc(sizeof *f + strlen(name) + 1);
>
> // Or do something clever with sprintf()
> memcpy(&f->prefix, foo_prefix, sizeof foo_prefix);
> memcpy(&f->name, name, strlen(name) + 1);
>
> return f;
> }
>
> For a given struct foo *f, I can access the full name as f->prefix, and
> I can access the unique part as f->name.

Suggestion: Combine `prefix` and `name` into a single array of char, and
use `name+offset` to refer to the portion of the string following the
prefix. Arrays are guaranteed to be contiguous.

If f->name contains a string whose length is at least 4, then f-name and
f->name+4 are both valid pointers to strings.

--
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: Padding between char array/VLA in struct?

<se9peg$5o4$1@z-news.wcss.wroc.pl>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.neostrada.pl!unt-exc-01.news.neostrada.pl!newsfeed.pionier.net.pl!pwr.wroc.pl!news.wcss.wroc.pl!not-for-mail
From: antis...@math.uni.wroc.pl
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 21:52:16 +0000 (UTC)
Organization: Politechnika Wroclawska
Lines: 46
Message-ID: <se9peg$5o4$1@z-news.wcss.wroc.pl>
References: <se9b5g$l2n$1@dont-email.me> <se9fa1$ikv$1@dont-email.me> <867dh34oia.fsf@linuxsc.com> <se9kev$ndj$1@dont-email.me>
NNTP-Posting-Host: hera.math.uni.wroc.pl
X-Trace: z-news.wcss.wroc.pl 1627941136 5892 156.17.86.1 (2 Aug 2021 21:52:16 GMT)
X-Complaints-To: abuse@news.pwr.wroc.pl
NNTP-Posting-Date: Mon, 2 Aug 2021 21:52:16 +0000 (UTC)
Cancel-Lock: sha1:dCC/Oi1CXboJOTMXH8aaDz64clg=
User-Agent: tin/2.4.3-20181224 ("Glen Mhor") (UNIX) (Linux/4.19.0-10-amd64 (x86_64))
 by: antis...@math.uni.wroc.pl - Mon, 2 Aug 2021 21:52 UTC

Ian Pilcher <arequipeno@gmail.com> wrote:
> On 8/2/21 2:47 PM, Tim Rentsch wrote:
> > I am curious to know why you care. Would having a guarantee that
> > there is no padding affect what code you would write? If it
> > would then there is a good chance that guarantee-depending code
> > has undefined behavior somewhere.
>
> It's just a convenient way to refer to either the "full" name (including
> the prefix) or the unique part of the name. For example, I've got a
> bunch of struct foo's, each of which has a name that starts with "FOO:",
> and most of the time I don't care about the prefix.
>
> #define FOO_PREFIX "FOO:"
>
> static const char foo_prefix[sizeof FOO_PREFIX - 1] = FOO_PREFIX;
>
> struct foo {
> /* other members */
> char prefix[sizeof foo_prefix];
> char name[];
> }
>
> struct foo *alloc_foo(char *name)
> {
> struct foo *f;
>
> f = malloc(sizeof *f + strlen(name) + 1);
>
> // Or do something clever with sprintf()
> memcpy(&f->prefix, foo_prefix, sizeof foo_prefix);
> memcpy(&f->name, name, strlen(name) + 1);
>
> return f;
> }
>
> For a given struct foo *f, I can access the full name as f->prefix,

No, you can not. Going above prefix length is undefined behaviour.
I know no "production" compiler that would detect it, but there
are various debugging aids and some should catch it.

> and
> I can access the unique part as f->name.

--
Waldek Hebisch

Re: Padding between char array/VLA in struct?

<se9umh$m1q$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 19:21:53 -0400
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <se9umh$m1q$2@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9c92$t69$1@dont-email.me>
<87a6lzael1.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 2 Aug 2021 23:21:53 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="87d7532726c8a615164838cbd807188d";
logging-data="22586"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19LaCMKZ0YwiFn5KzbqElJzf6Rspj/xGkk="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:x/gchUzNj6DcpjfM2HgkFUa9nfM=
In-Reply-To: <87a6lzael1.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: James Kuyper - Mon, 2 Aug 2021 23:21 UTC

On 8/2/21 2:25 PM, Keith Thompson wrote:
> James Kuyper <jameskuyper@alumni.caltech.edu> writes:
>> On 8/2/21 1:48 PM, Ian Pilcher wrote:
>>> Given the following:
>>>
>>> struct foo {
>>> /* other members */
>>
>> Note: this is a VLA unless PREFIX_SIZE is a macro that expands to an
>> integer constant expression.
>>
>>> char prefix[PREFIX_SIZE];
>
> This cannot be a VLA because VLAs are not allowed as struct or union members
You're right - I suspected that the thing he incorrectly thought was a
VLA was "name", but I wanted to cover the possibility that it was
"prefix". In my haste, I mentioned only one of the reasons (and the less
important one, to boot), why it wouldn't be one.

Re: Padding between char array/VLA in struct?

<seaatu$kii$1@dont-email.me>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Mon, 2 Aug 2021 19:50:37 -0700
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <seaatu$kii$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
<se9jh0$gre$1@dont-email.me> <87o8af8u7v.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 3 Aug 2021 02:50:38 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="573e1c1bf9197f13b7530f8f0e9f5dfe";
logging-data="21074"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ncbgm0FAULR5v41lZz8B5"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:AV3i8RAZcicHiELLQhdjjG1uKcg=
In-Reply-To: <87o8af8u7v.fsf@nosuchdomain.example.com>
Content-Language: en-US
 by: Andrey Tarasevich - Tue, 3 Aug 2021 02:50 UTC

On 8/2/2021 1:31 PM, Keith Thompson wrote:
>
> No, gcc's extension appears to allow VLAs as struct members. This
> compiles and runs without error and, as expected, prints different
> values when it's executed again after more than a second:
>
> #include <stdio.h>
> #include <time.h>
> #include <stddef.h>
> int main(void) {
> const int size = time(NULL) % 10 + 10;
> struct s {
> char s0[size];
> char s1[size];
> char s2[size];
> };
>
> struct s obj;
>
> printf("sizeof obj = %zu\n", sizeof obj);
> printf("%zu %zu %zu\n",
> offsetof(struct s, s0),
> offsetof(struct s, s1),
> offsetof(struct s, s2));
> }
>
> (I didn't find this extension in gcc's documentation, but I didn't look
> very hard.)
>

Oh, indeed... I missed that. Thank you for pointing it out.

Interestingly, when GCC has to deal with a bunch of independent VLAs

char s0[n0];
char s1[n1];
char s2[n2];

in the generated code it typically implements them as simple individual
`char *` pointers, pointing into memory allocated by `alloca`-like
mechanism.

But once they are bundled into a struct

struct s
{
char s0[n0];
char s1[n1];
char s2[n2];
} obj;

GCC no longer has a luxury of implementing each array as an individual
pointer. It wants to maintain a flat memory layout for a struct object,
which forces it to generate code that allocates a memory buffer of size
`n0+n1+n2` and then use arithmetic on-the-fly to locate each individual
array in that buffer, e.g. to locate the beginning of `s2` it has to
calculate `n0+n1`.

In other words, the latter implementation does not come "for free": it
is markedly different from the former.

--
Best regards,
Andrey Tarasevich

Re: Padding between char array/VLA in struct?

<sebo28$ig7$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: cr88...@gmail.com (BGB)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Tue, 3 Aug 2021 10:40:54 -0500
Organization: A noiseless patient Spider
Lines: 91
Message-ID: <sebo28$ig7$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 3 Aug 2021 15:40:56 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b1fad58203ab748bcc18c966ec3dcd45";
logging-data="18951"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/tARLME+I6ubKI0KkUPyMa"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:ebuvVJM1qJwmOI5lLh/1c8RXWsE=
In-Reply-To: <se9htr$5ll$1@dont-email.me>
Content-Language: en-US
 by: BGB - Tue, 3 Aug 2021 15:40 UTC

On 8/2/2021 2:43 PM, Bart wrote:
> On 02/08/2021 19:27, Keith Thompson wrote:
>> Bart <bc@freeuk.com> writes:
>
>>> BTW there is no VLA here unless PREFIX_SIZE is not a compile-time
>>> expression (and I'm not sure even then).
>>
>> If PREFIX_SIZE is not a compile time expression, it's a constraint
>> violation.  Struct and union member cannot be VLAs.  (Also, VLA support
>> is optional as of C11.)
>>
>
> This program works on gcc, tcc and lccwin:
>
> #include <stdio.h>
> int main(void) {
>     int n=100;
>     typedef struct{int xx[n];} S;
>
>     S a;
>
>     printf("sizeof(S) = %d\n",(int)sizeof(S));
> }
>
> The output is respectively 400, 8 and 32.
>
> It fails with DMC, clang, msvc and bcc (but the last two don't support
> VLAs anyway).
>

This will not likely work correctly in my compiler either, unless a
special case provision were made for the last member in a struct (the
expression would be ignored in this case).

> The mind boggles as to how it's implemented, for example with static,
> auto and heap-allocated arrays of S, or with linked lists using S nodes,
> or when S itself contains structs with such VLAs.

FWIW: My compiler (BGBCC) also supports VLAs, though only for local
variables and similar. In these contexts, they effectively decay into
pointers, and are transformed into an "alloca()" call.

Potentially, a static or global VLA could be transformed into creative
use of "realloc" or similar, or a wrapper around realloc, eg:
void __vla_globalvla(void **rptr, size_t *rsz, size_t newsz);

Which then resizes the array to be bigger if the new size exceeds the
old size, and would probably be called (potentially repeatedly) by any
function which tries to access such an array.

But, as can be noted, my compiler doesn't do anything too fancy to
support "alloca()"; in effect this gets turned by the compiler into a
special runtime call for "call malloc() and then add the returned
pointer to a linked list", and then in the epilog a function call is
added to free anything that was added to the list.

I also make no claim that multidimensional VLAs "actually work"...

Note that for normal functions with arrays, the compiler may also add
special guard tokens, and then trigger a breakpoint on function return
if these don't match their expected values. This partly helps detect if
an array overrun has occurred, but may also help detect cases where the
stack-pointer got messed up or similar.

....

My compiler also recently added lambdas, which effectively make use of
the same mechanism as "alloca()" (though does use a different runtime
call so that the returned memory can be marked as executable).

The spec for lambdas more-or-less follows the C23 proposal, but there is
an alternate syntax as well which allows for (among other things),
creating lambdas with an unbounded lifespan (though with the restriction
that they will leak memory unless manually freed).

In this case, the lambda is basically a struct, with the first 32-bytes
(at present) reserved for setting up the registers and calling into the
function body (the current stub uses 28 bytes).

The encodings used for the stub is slightly conservative, but using a
few newer encodings, it could be possible to cram it down to 12 bytes.
However, the encodings in question are not currently part of the
"baseline" ISA, and in practice this isn't likely to make much real
difference on either performance or memory footprint.

....

Re: Padding between char array/VLA in struct?

<sebpqf$utm$1@dont-email.me>

  copy mid

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

  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: Padding between char array/VLA in struct?
Date: Tue, 3 Aug 2021 09:10:54 -0700
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <sebpqf$utm$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
<sebo28$ig7$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 3 Aug 2021 16:10:55 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="c25a9efebf76a55453ff805aa3a214b0";
logging-data="31670"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Wjpns+mHWXa+BXkzYq1Gj"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:DvDpR4dYOl0easwoz56k2cdeq64=
In-Reply-To: <sebo28$ig7$1@dont-email.me>
Content-Language: en-US
 by: Andrey Tarasevich - Tue, 3 Aug 2021 16:10 UTC

On 8/3/2021 8:40 AM, BGB wrote:
>
> FWIW: My compiler (BGBCC) also supports VLAs, though only for local
> variables and similar. In these contexts, they effectively decay into
> pointers, and are transformed into an "alloca()" call.
> ...
> But, as can be noted, my compiler doesn't do anything too fancy to
> support "alloca()"; in effect this gets turned by the compiler into a
> special runtime call for "call malloc() and then add the returned
> pointer to a linked list", and then in the epilog a function call is
> added to free anything that was added to the list.
>
> I also make no claim that multidimensional VLAs "actually work"...
> ...

It appears that the amount of effort you spent implementing your
pseudo-VLA would be quite sufficient to implement the standard-compliant
VLA. You just needed to channel that effort slightly differently. Which
makes one wonder why you decided to eschew the standard semantics...

--
Best regards,
Andrey Tarasevich

Re: Padding between char array/VLA in struct?

<sebvpf$bfs$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: cr88...@gmail.com (BGB)
Newsgroups: comp.lang.c
Subject: Re: Padding between char array/VLA in struct?
Date: Tue, 3 Aug 2021 12:52:45 -0500
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <sebvpf$bfs$1@dont-email.me>
References: <se9b5g$l2n$1@dont-email.me> <se9cfv$uta$1@dont-email.me>
<875ywnaehw.fsf@nosuchdomain.example.com> <se9htr$5ll$1@dont-email.me>
<sebo28$ig7$1@dont-email.me> <sebpqf$utm$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 3 Aug 2021 17:52:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="b1fad58203ab748bcc18c966ec3dcd45";
logging-data="11772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/52kryYH26ZsKMxe5Hg2Kr"
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
Cancel-Lock: sha1:ycC3sEBMxB+l2YO6nAuqlMV+otc=
In-Reply-To: <sebpqf$utm$1@dont-email.me>
Content-Language: en-US
 by: BGB - Tue, 3 Aug 2021 17:52 UTC

On 8/3/2021 11:10 AM, Andrey Tarasevich wrote:
> On 8/3/2021 8:40 AM, BGB wrote:
>>
>> FWIW: My compiler (BGBCC) also supports VLAs, though only for local
>> variables and similar. In these contexts, they effectively decay into
>> pointers, and are transformed into an "alloca()" call.
>> ...
>> But, as can be noted, my compiler doesn't do anything too fancy to
>> support "alloca()"; in effect this gets turned by the compiler into a
>> special runtime call for "call malloc() and then add the returned
>> pointer to a linked list", and then in the epilog a function call is
>> added to free anything that was added to the list.
>>
>> I also make no claim that multidimensional VLAs "actually work"...
>> ...
>
> It appears that the amount of effort you spent implementing your
> pseudo-VLA would be quite sufficient to implement the standard-compliant
> VLA. You just needed to channel that effort slightly differently. Which
> makes one wonder why you decided to eschew the standard semantics...
>

Partly it was because this was what seemed easier, and also allowed
doing pretty much all of the VLA stuff to be handled in the front-end
(things like lambdas are also handled in the frontend, as are many
operations involving "variant" types, *1, ...).

With the ABI design, variable-sized stack frames would have added more
complexity relative to fixed-size stack frames (there is no frame
pointer or similar in this case, and everything on-stack is referenced
using fixed displacements relative to the stack pointer, and
prolog/epilog adjustments use fixed offsets, ...).

Note that this is generally for a target where:
I am using 128K as the default stack size;
(Except for interrupt handlers, which use an 8K stack).
The target is (or assumes) No-MMU operation;
The RAM space is typically measured in MB.

Using malloc ends up preferable for larger memory allocations (VLAs or
large stack arrays), since it isn't bound by the stack-size limit, and
doesn't assume spending inordinate amounts of RAM on the program stacks.

Though, a few cases exist where the total RAM is measured in KB (and the
heap is just sorta wedged between ".bss" and the stack).

*1: The variant types are a non-standard extension which add dynamic
type-checking via tagged pointers; and turns pretty much every operation
on these types into a function call into the runtime.

....


devel / comp.lang.c / Re: Padding between char array/VLA in struct?

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor