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


Groups > comp.lang.c > #41456

Re: Padding involved

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: Padding involved
Date 2014-03-08 01:03 +0000
Organization Aioe.org NNTP Server
Message-ID <20140307165348.597@kylheku.com> (permalink)
References (2 earlier) <54bf1188-f3d7-409f-a15a-522cb3038333@googlegroups.com> <lfdjmp$jmh$1@dont-email.me> <602c31f6-8abb-4ddb-b64a-4c681c404659@googlegroups.com> <lnr46dv8ol.fsf@nuthaus.mib.org> <6c398cac-bf10-4213-b147-4a01ac23568f@googlegroups.com>

Show all headers | View raw


On 2014-03-08, anish kumar <yesanishhere@gmail.com> wrote:
> "structures and unions assume the alignment of their most strictly
> aligned component. Each member is assigned to the lowest available
> offset with the appropriate alignment. The size of the object is always
> a multiple of the object's alignment."
>
> I couldn't understand the above statement.

This simply means that an N byte type has an alignment requirement to be on an
offset divisible by N. And when a structure member of size N is being
allocated, the next available offset that is divisible by N is chosen (the
"lowest available offset witha ppropriate alignment").

The "structures and unions assume the alignment of their most strictly aligned
component" means that if the structure contains an element of size N, then
there is enough padding at the end of the structure so that this element
will be correctly aligned as an array member.

For instance 

  struct foo { char a; long long b; char c; }

char a is at offset zero. Then 64 bit wide b goes to an address divisible
by 8, leaving a padding of 7, bringing us to 16 bytes. Now c is
allocated to the 17th byte.  17 cannot be the final size, because
in an array struct foo x[2],  x[1].b will end up on a funny address!

The "most strictly aligned component" is b: aligned to 8 byte boundaries.  And
so, the structure must be padded so its size is divisible by 8: matching the
alignment requirement of the most strictly aligned component.  The size will be
the next available multiple of 8 after 17: 24.  Seven bytes of padding, again.

Also, the malloc function is required to return pointers that are at
least as strictly aligned as any basic data type.

In the structure layout, offset 0 is asssumed to be suitably aligned
for anything. Regardless of type, the first member is placed at offset
zero without any padding at the start.  The allocator has to make that
assumption true.

Not only malloc, but the compiler and linker: how they lay out objects
in static storage and in automatic storage (the stack).

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


Thread

Padding involved anish singh <anish198519851985@gmail.com> - 2014-03-07 13:13 -0800
  Re: Padding involved jacob navia <jacob@spamsink.net> - 2014-03-07 22:17 +0100
  Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-07 16:26 -0500
    Re: Padding involved anish singh <anish198519851985@gmail.com> - 2014-03-07 13:49 -0800
      Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-07 18:11 -0500
        Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 15:19 -0800
          Re: Padding involved Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-03-07 16:26 -0700
            Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 15:41 -0800
              Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-07 18:50 -0500
            Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-08 00:10 +0000
              Re: Padding involved Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2014-03-08 09:27 -0700
          Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-07 16:24 -0800
            Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 16:45 -0800
              Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-08 01:03 +0000
                Re: Padding involved David Thompson <dave.thompson2@verizon.net> - 2014-03-28 06:11 -0400
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-28 18:45 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-28 15:23 -0400
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-28 12:40 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-28 21:19 +0000
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-28 17:07 -0500
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-28 18:25 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-28 21:15 -0500
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-29 01:03 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 14:23 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-30 01:43 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-28 21:32 -0400
                Re: Padding involved Richard Damon <Richard@Damon-Family.org> - 2014-03-28 21:59 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-29 00:46 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-29 08:51 -0400
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-29 08:57 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-30 18:53 -0500
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-30 20:25 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-31 11:34 -0500
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-31 11:27 -0700
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-31 11:36 -0700
                Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-31 19:14 +0000
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-31 15:43 -0500
                Re: Padding involved Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-04-01 03:12 +0300
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-04-03 00:45 -0500
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-29 13:02 -0500
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-29 14:44 -0400
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-29 19:49 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-30 19:12 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-31 16:20 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-31 17:53 -0400
                Re: Padding involved Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2014-04-01 03:22 +0300
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-04-04 14:36 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-04-04 17:33 -0400
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 13:46 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-04-14 22:00 +0000
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-04-14 15:37 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-17 09:11 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 13:25 -0700
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-29 00:57 -0700
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-29 08:57 -0400
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-29 14:18 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-30 12:02 -0700
                Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-30 19:35 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-04-14 12:54 -0700
                Re: Padding involved Tim Rentsch <txr@alumni.caltech.edu> - 2014-03-29 14:44 -0700
                Re: Padding involved glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2014-03-29 03:50 +0000
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-29 09:02 -0400
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-29 13:19 -0500
                Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-28 21:21 -0400
                Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-28 15:27 -0400
                Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-28 19:54 +0000
                Re: Padding involved Stephen Sprunk <stephen@sprunk.org> - 2014-03-28 15:02 -0500
          Re: Padding involved Kaz Kylheku <kaz@kylheku.com> - 2014-03-08 00:43 +0000
          Re: Padding involved Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-03-07 20:20 -0500
    Re: Padding involved anish singh <anish198519851985@gmail.com> - 2014-03-07 13:49 -0800
  Re: Padding involved James Kuyper <jameskuyper@verizon.net> - 2014-03-07 16:46 -0500
  Re: Padding involved "BartC" <bc@freeuk.com> - 2014-03-07 21:59 +0000
    Re: Padding involved anish kumar <yesanishhere@gmail.com> - 2014-03-07 14:34 -0800
      Re: Padding involved "BartC" <bc@freeuk.com> - 2014-03-07 23:59 +0000
    Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-07 15:15 -0800
      Re: Padding involved "BartC" <bc@freeuk.com> - 2014-03-08 10:08 +0000
        Re: Padding involved Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-08 12:39 +0000
        Re: Padding involved Keith Thompson <kst-u@mib.org> - 2014-03-08 14:03 -0800
  Re: Padding involved Malcolm McLean <malcolm.mclean5@btinternet.com> - 2014-03-08 09:01 -0800

csiph-web