Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #162207

Re: Padding between char array/VLA in struct?

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.lang.c
Subject Re: Padding between char array/VLA in struct?
Date 2021-08-02 13:15 -0700
Organization None to speak of
Message-ID <871r7ba9hq.fsf@nosuchdomain.example.com> (permalink)
References <se9b5g$l2n$1@dont-email.me> <87eebbaf6w.fsf@nosuchdomain.example.com> <se9d6b$4d0$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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