[lkml]   [2007]   [Nov]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
    SubjectRe: [RFC] Documentation about unaligned memory access
    On Thursday 22 November 2007 04:15:53 pm Daniel Drake wrote:
    > Fortunately things are not too complex, as in most cases, the compiler
    > ensures that things will work for you. For example, take the following
    > structure:
    > struct foo {
    > u16 field1;
    > u32 field2;
    > u8 field3;
    > };
    > Fortunately, the compiler understands the alignment constraints, so in the
    > above case it would insert 2 bytes of padding inbetween field1 and field2.
    > Therefore, for standard structure types you can always rely on the compiler
    > to pad structures so that accesses to fields are suitably aligned (assuming
    > you do not cast the field to a type of different length).

    It would also insert 3 bytes of padding after field3, in order to satisfy
    alignment constraints for arrays of these structures.

    > Sidenote: in the above example, you may wish to reorder the fields in the
    > above structure so that the overall structure uses less memory. For
    > example, moving field3 to sit inbetween field1 and field2 (where the
    > padding is inserted) would shrink the overall structure by 1 byte:
    > struct foo {
    > u16 field1;
    > u8 field3;
    > u32 field2;
    > };

    It will actually shrink it by 4 bytes, for the very same reason.

    -- Vadim Lobanov

    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to
    More majordomo info at
    Please read the FAQ at

     \ /
      Last update: 2007-11-23 23:05    [W:0.022 / U:62.444 seconds]
    ©2003-2017 Jasper Spaans. hosted at Digital OceanAdvertise on this site