unaligned pointer access

Discussion in 'C Programming' started by Sven Köhler, Sep 11, 2013.

  1. (snip)
    There are 32 bit (address width) processors with a 64 bit data bus,
    such that 64 bit aligned load/store are faster for 64 bit data,
    than unaligned load/store.

    The ones I know of don't require alignment, but it is faster.

    -- glen
     
    glen herrmannsfeldt, Sep 12, 2013
    #21
    1. Advertisements

  2. Sven Köhler

    James Kuyper Guest

    On 09/11/2013 01:45 PM, James Kuyper wrote:
    ....
    The next 3 _Alignof()s were supposed to be _Alignas():
     
    James Kuyper, Sep 12, 2013
    #22
    1. Advertisements

  3. If an object of type int64_t is allocated in a way that lets the
    compiler control its alignment (say, if it's a declared object, a
    malloc()ed object, or a subobject of such an object), it will be
    properly aligned. Just what that means depends on the implementation;
    one implementation might require 8-byte alignment, another might not
    require any particular alignment.

    If a chunk of memory that's not such an object is treated as an object
    of type int64_t, then the behavior is undefined unless it's properly
    aligned (and on some systems, a 4-byte aligned int64_t is not properly
    aligned).
     
    Keith Thompson, Sep 12, 2013
    #23
  4. But the use of the __attribute((packed)) or #pragma pack can lead
    to incorrect code. The compiler can detect misaligned accesses that
    refer to the member name, but if you take the address of a misaligned
    member and later dereference that pointer, your program can crash.

    http://stackoverflow.com/q/8568432/827263
     
    Keith Thompson, Sep 12, 2013
    #24
  5. Am 12.09.2013 03:11, schrieb James Kuyper:
    I see, that makes sense! Thanks.


    Regards,
    Sven
     
    Sven Köhler, Sep 12, 2013
    #25
  6. Sven Köhler

    Eric Sosman Guest

    C11 adds a requirement that the alignment be a power of two.
    A ten-byte `long double' is okay, but not if it needs five-byte
    alignment. ;)
    Hadn't thought of that one. Interesting corner case.
     
    Eric Sosman, Sep 12, 2013
    #26
  7. Sven Köhler

    Sven Köhler Guest

    To the best of my knowledge, the size of a struct is always padded to be
    a multiple of the alignments of the members. I.e. if you have a struct
    of an int (size 4) and a char in that order, then the struct will be
    padded to size 8 if the int requires alignment 4.


    Regards,
    Sven
     
    Sven Köhler, Sep 24, 2013
    #27
  8. Yeah, "portable" pretty much went out the window by the second paragraph.
     
    Edward A. Falk, Sep 25, 2013
    #28
  9. Best bet is to use memcpy() to move the data to a properly-aligned
    variable, and hope that the compiler replaces the memcpy() with
    something more efficient for the architecture. There might be
    non-portable techniques that are more efficient, but unless this
    is critical inner-loop stuff, it's almost certainly not worth it.
     
    Edward A. Falk, Sep 25, 2013
    #29
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.