Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162212
| Path | csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <Keith.S.Thompson+u@gmail.com> |
| 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> (permalink) |
| 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= |
| Xref | csiph.com comp.lang.c:162212 |
Show key headers only | View raw
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 */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Padding between char array/VLA in struct? Ian Pilcher <arequipeno@gmail.com> - 2021-08-02 12:48 -0500
Re: Padding between char array/VLA in struct? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-08-02 14:07 -0400
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 11:25 -0700
Re: Padding between char array/VLA in struct? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-08-02 19:21 -0400
Re: Padding between char array/VLA in struct? Bart <bc@freeuk.com> - 2021-08-02 19:11 +0100
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 11:27 -0700
Re: Padding between char array/VLA in struct? Bart <bc@freeuk.com> - 2021-08-02 20:43 +0100
Re: Padding between char array/VLA in struct? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-08-02 13:11 -0700
Re: Padding between char array/VLA in struct? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-08-02 13:18 -0700
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 13:31 -0700
Re: Padding between char array/VLA in struct? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-08-02 19:50 -0700
Re: Padding between char array/VLA in struct? David Brown <david.brown@hesbynett.no> - 2021-08-04 14:01 +0200
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 13:19 -0700
Re: Padding between char array/VLA in struct? BGB <cr88192@gmail.com> - 2021-08-03 10:40 -0500
Re: Padding between char array/VLA in struct? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-08-03 09:10 -0700
Re: Padding between char array/VLA in struct? BGB <cr88192@gmail.com> - 2021-08-03 12:52 -0500
Re: Padding between char array/VLA in struct? BGB <cr88192@gmail.com> - 2021-08-04 12:05 -0500
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 11:12 -0700
Re: Padding between char array/VLA in struct? Bart <bc@freeuk.com> - 2021-08-02 19:23 +0100
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 13:15 -0700
Re: Padding between char array/VLA in struct? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-08-02 11:32 -0700
Re: Padding between char array/VLA in struct? Ian Pilcher <arequipeno@gmail.com> - 2021-08-02 13:59 -0500
Re: Padding between char array/VLA in struct? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-02 12:47 -0700
Re: Padding between char array/VLA in struct? Ian Pilcher <arequipeno@gmail.com> - 2021-08-02 15:27 -0500
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 13:36 -0700
Re: Padding between char array/VLA in struct? antispam@math.uni.wroc.pl - 2021-08-02 21:52 +0000
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-02 13:22 -0700
Re: Padding between char array/VLA in struct? Manfred <noname@add.invalid> - 2021-08-04 17:01 +0200
Re: Padding between char array/VLA in struct? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-08-04 10:11 -0700
csiph-web