Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162217
| From | BGB <cr88192@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Padding between char array/VLA in struct? |
| Date | 2021-08-03 10:40 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <sebo28$ig7$1@dont-email.me> (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> |
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.
...
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