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


Groups > comp.lang.c > #30317

Re: Data alignment questin, structures

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.c
Subject Re: Data alignment questin, structures
Date 2013-01-18 14:46 -0500
Organization A noiseless patient Spider
Message-ID <kdc8q8$ei0$1@dont-email.me> (permalink)
References <kcs982$r3p$1@dont-email.me> <kcshq5$7e8$1@dont-email.me> <kdc426$f0i$1@dont-email.me>

Show all headers | View raw


On 1/18/2013 1:24 PM, mathog wrote:
> Eric Sosman wrote:
>>> Now, what happens when this struct is embedded in a binary file stored
>>> in a character array "buffer[]" such that its position "i" is not
>>> guaranteed to be aligned on the same boundary that
>>>
>>>    Mystract *instance2 = malloc(sizeof(Mystruct));
>>>
>>> would have used, but will always be on a multiple of 2.
>>>
>>> 1.  Will this always work?
>>>
>>>    function1((Mystruct *) &(buffer[i]));
>>
>>      Not "always," no.  6.3.2.3p7: "A pointer to an object type
>> may be converted to a pointer to a different object type. If the
>> resulting pointer is not correctly aligned for the referenced
>> type, the behavior is undefined. [...]"  There's no guarantee
>> that the alignment of `buffer' suffices for `Mystruct'.
>
> On further consideration, how does one know what "correctly aligned"
> means for a given struct?

     As of C11, the _Alignof operator tells you.  In earlier C's
alignment requirements are implementation-defined, and all you
could tell for sure was that the alignment requirement for a
type T had to be an exact divisor of sizeof(T).

> Consider these two examples:
>
> typedef struct {
>     uint16_t one;
>     uint16_t two;
>     uint16_t three;
>     uint16_t four;
> } Mystruct4;
>
> typedef struct {
>     uint16_t one;
>     uint16_t two;
>     uint16_t three;
>     uint16_t four;
>     uint16_t five;
> } Mystruct5;
>
> The first would presumably be 8 bytes and the second 10.

     If uint16_t exists, it follows that CHAR_BIT must be 8 or 16.
Then the minimum sizes of the first struct is 8 or 4, and of the
second 5 or 10.  But these are minima: Padding is permitted after
any struct element (let's ignore bit-fields), for any reason the
implementor finds convincing.

     Even so, I'd be extremely surprised if the size of the first
struct were not 8 or 4.  I'd be only mildly surprised, though, if
the size of the second were 12 or 6.

> Is it safe to
> access these in a memory buffer via a pointer so long as that pointer
> is aligned on a 2 byte boundary?  I would assume yes because all of the
> data within each struct has that alignment,

     The alignment of a struct (or union) is *at least* as strict
as the maximum alignment of any of its elements, but may be stricter.

> and I am also assuming that
> this code should always work:
>
> Mystruct5  array[10];  /* valid */
> Mystruct5  *aptr;
> uint16_t   x;
>        array[1].four=20;
>        aptr = &array[1];
>        /* the next three lines should all print: 1,4 is:20 */
>        printf("1,4 is:%u\n",aptr->five);
>        somefunction1(aptr);
>        somefunction2(*aptr);
>
> ...
>
> void somefunction1(Mystruct5 *aptr){
>        printf("1,4 is:%u\n",aptr->four);
> }
> void somefunction2(Mystruct5 aptr){
>        printf("1,4 is:%u\n",aptr.four);
> }

     Yes, this will always produce the expected output (once the
typo in the first printf() is corrected and suitable scaffolding
is added).  But I don't see what bearing it has on matters of
alignment; could you elucidate?

> Or may a compiler make some assumption for Mystruct4 that requires it to
> be aligned on a 4 byte boundary too?

     Yes.  It might even require 8-byte alignment.  In theory, the
compiler might insert 1016 padding bytes and require 1024-byte
alignment (this is the same theory that predicts the nasal demon).

> That would not prevent it from
> being used in an array, and would not break the above example (after
> changing Mystruct5 -> Mystruct4).  But it would break both of the two
> function calls if aptr was pointing to a buffer in memory where the data
> was only 2 byte aligned for the structure.

     Right.

> Conversely, adding a 4 byte
> boundary alignment requirement on Mystruct5 would add two pad bytes, for
> no obvious reason, but it would not break the above code example.  I see
> no reason for the compiler to add the 4 byte alignment
> requirement for these structures, but is it nevertheless free to do so?

     You and I see no pressing need for padding in these structs,
but the implementor might see one (or imagine he sees one) -- and
he's got the only vote.

-- 
Eric Sosman
esosman@comcast-dot-net.invalid

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


Thread

Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-12 10:15 -0800
  Re: Data alignment questin, structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-12 18:54 +0000
    Re: Data alignment questin, structures Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-01-15 20:14 +0000
      Re: Data alignment questin, structures Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-15 16:29 -0500
  Re: Data alignment questin, structures Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-12 15:41 -0500
    Re: Data alignment questin, structures mathog <dmathog@gmail.com> - 2013-01-18 10:24 -0800
      Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-18 14:05 -0500
      Re: Data alignment questin, structures Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-01-18 14:46 -0500
      Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-18 21:11 +0000
      Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-18 17:14 -0500
        Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-18 23:09 +0000
      Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-18 16:23 -0800
      Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-19 21:02 -0800
        Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-20 01:25 -0500
        Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 07:53 +0000
        Re: Data alignment questin, structures Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-20 14:02 +0000
        Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-20 07:35 -0800
          Re: Data alignment questin, structures 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-20 11:49 -0800
        Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-20 12:30 -0500
          Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-20 10:26 -0800
            Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-20 15:11 -0500
            Re: Data alignment questin, structures "BartC" <bc@freeuk.com> - 2013-01-20 20:34 +0000
            Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-20 15:53 -0500
              Re: Data alignment questin, structures 88888 Dihedral <dihedral88888@googlemail.com> - 2013-01-20 13:16 -0800
              Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-20 20:27 -0800
                Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-21 00:37 -0500
                Re: Data alignment questin, structures Dr Nick <nospam-4@temporary-address.org.uk> - 2013-01-21 10:07 +0000
                Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-21 10:21 -0800
                Re: Data alignment questin, structures Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-21 10:34 -0800
                Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-21 13:07 -0800
                Re: Data alignment questin, structures "BartC" <bc@freeuk.com> - 2013-01-21 22:24 +0000
                Re: Data alignment questin, structures mathog <dmathog@gmail.com> - 2013-01-22 10:18 -0800
                Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-22 20:57 +0000
                Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-22 14:51 -0800
                Re: Data alignment questin, structures Ian Collins <ian-news@hotmail.com> - 2013-01-22 11:12 +1300
                Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-21 20:53 +0000
                Re: Data alignment questin, structures Keith Thompson <kst-u@mib.org> - 2013-01-20 22:06 -0800
                Re: Data alignment questin, structures Philip Lantz <prl@canterey.us> - 2013-01-22 00:27 -0800
                Re: Data alignment questin, structures James Kuyper <jameskuyper@verizon.net> - 2013-01-22 06:44 -0500
                Re: Data alignment questin, structures Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> - 2013-01-21 10:19 +0000
            Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 22:19 +0000
          Re: Data alignment questin, structures glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-20 22:14 +0000
          Re: Data alignment questin, structures "BartC" <bc@freeuk.com> - 2013-01-20 22:48 +0000
            Re: Data alignment questin, structures Les Cargill <lcargill99@comcast.com> - 2013-01-20 18:11 -0600
            Re: Data alignment questin, structures Ian Collins <ian-news@hotmail.com> - 2013-01-21 14:07 +1300
  Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-12 17:45 -0500
    Re: Data alignment questin, structures mathog <mathog@caltech.edu> - 2013-01-13 10:17 -0800
      Re: Data alignment questin, structures Shao Miller <sha0.miller@gmail.com> - 2013-01-13 17:16 -0500

csiph-web