Why is a double member in struct not aligned on 8 byte boundary?

前端 未结 3 828
既然无缘
既然无缘 2021-01-13 10:06

This is about memory alignment. In code below, I expected that the offset of b inside the structure to be 8 (32-bit machine). See here. There by, making b alway

3条回答
  •  青春惊慌失措
    2021-01-13 10:46

    There is no guarantee about alignment, at all, in ANSI C.

    The alignment happens with automatic variables more than with anything declared on the heap. If you're on a POSIX OS, use memalign(3) to receive memory that you're sure is aligned. Malloc may return memory at any offset. You can use compiler directives like __attribute__ ((__packed__)) to put in your own alignments.

提交回复
热议问题