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


Groups > comp.lang.c > #106837

Re: struct padding question

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: struct padding question
Date 2017-03-23 19:57 +0100
Organization A noiseless patient Spider
Message-ID <ob15m8$4ef$1@dont-email.me> (permalink)
References (3 earlier) <ob0iif$qti$1@dont-email.me> <ob0jei$t0t$1@dont-email.me> <ob0kmg$2ut$1@dont-email.me> <ob0n96$cah$1@dont-email.me> <VsTAA.128247$Bp6.93731@fx36.iad>

Show all headers | View raw


On 23/03/17 18:19, Scott Lurndal wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 23/03/17 15:07, BartC wrote:
>
>>> It would funny if the compiler gave a warning about struct padding which
>>> does no harm, but doesn't do so about necessary padding being omitted.
>>>
>>
>> If the padding is /necessary/ for a given target, the compiler will not
>> omit it.  It might be necessary for the logic of the program, but that
>> is up to the programmer, not the compiler.
>
> The padding is generally /necessary/ for performance, not correctness,
> as most modern architectures support unaligned accesses (albeit often
> with a performance penalty).   On those that don't support unaligned
> accesses (e.g. intel with the AF flag set, or 32-bit arm), the padding
> is necessary for correctness.
>
> There are a few cases where packing is required for correctness, generally
> to match some existing data structure or to interoperate with existing
> applications.
>

Padding can be necessary for correctness for two reasons.  One is to 
avoid unaligned access (and I work with several different architectures 
- there a number that don't support unaligned access).  The other is 
because your struct has to match an existing struct definition exactly, 
such as a file structure, an filesystem structure, a network packet 
format, or a set of hardware registers (which is the most common case 
for my work).  In these cases, the struct has to match precisely - with 
as much or as little padding as necessary.

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


Thread

struct padding question MehdiAmini <atorrses@gmail.com> - 2017-03-23 14:28 +0430
  Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 11:28 +0100
    Re: struct padding question MehdiAmini <atorrses@gmail.com> - 2017-03-23 16:44 +0430
      Re: struct padding question jm.bourguet@gmail.com - 2017-03-23 05:30 -0700
      Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-03-23 08:45 -0700
    Re: struct padding question Noob <root@127.0.0.1> - 2017-04-01 19:15 +0200
      Re: struct padding question tim prince <n8tm@aol.com> - 2017-04-01 14:41 -0400
        Re: struct padding question supercat@casperkitty.com - 2017-04-02 09:15 -0700
          Re: struct padding question scott@slp53.sl.home (Scott Lurndal) - 2017-04-03 13:51 +0000
            Re: struct padding question supercat@casperkitty.com - 2017-04-03 07:48 -0700
              Re: struct padding question scott@slp53.sl.home (Scott Lurndal) - 2017-04-03 15:47 +0000
                Re: struct padding question supercat@casperkitty.com - 2017-04-03 09:24 -0700
      Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-04-01 11:43 -0700
      Re: struct padding question Robert Wessel <robertwessel2@yahoo.com> - 2017-04-01 13:46 -0500
        Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-04-01 12:00 -0700
          Re: struct padding question Robert Wessel <robertwessel2@yahoo.com> - 2017-04-01 16:40 -0500
            Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-04-01 15:24 -0700
              Re: struct padding question Robert Wessel <robertwessel2@yahoo.com> - 2017-04-03 17:46 -0500
  Re: struct padding question BartC <bc@freeuk.com> - 2017-03-23 11:50 +0000
    Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 14:08 +0100
      Re: struct padding question BartC <bc@freeuk.com> - 2017-03-23 13:31 +0000
        Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 14:46 +0100
          Re: struct padding question BartC <bc@freeuk.com> - 2017-03-23 14:07 +0000
            Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 15:52 +0100
              Re: struct padding question scott@slp53.sl.home (Scott Lurndal) - 2017-03-23 17:19 +0000
                Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 19:57 +0100
      Re: struct padding question Ian Pilcher <arequipeno@gmail.com> - 2017-03-23 08:52 -0500
        Re: struct padding question David Brown <david.brown@hesbynett.no> - 2017-03-23 15:54 +0100
          Re: struct padding question supercat@casperkitty.com - 2017-03-23 08:29 -0700
  Re: struct padding question GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-23 14:04 +0100
    Re: struct padding question MehdiAmini <atorrses@gmail.com> - 2017-03-23 20:32 +0430
      Re: struct padding question GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-23 18:35 +0100
      Re: struct padding question supercat@casperkitty.com - 2017-03-23 12:02 -0700
        Re: struct padding question Keith Thompson <kst-u@mib.org> - 2017-03-23 12:21 -0700
          Re: struct padding question supercat@casperkitty.com - 2017-03-23 12:55 -0700
  Re: struct padding question Les Cargill <lcargill99@comcast.com> - 2017-03-25 15:12 -0500

csiph-web