memory alignment within gcc structs

前端 未结 6 493
情歌与酒
情歌与酒 2020-11-29 05:38

I am porting an application to an ARM platform in C, the application also runs on an x86 processor, and must be backward compatible.

I am now having some issues with

6条回答
  •  有刺的猬
    2020-11-29 06:18

    I would guess that the problem is that 42 isn't divisible by 4, and so they get out of alignment if you put several of these structs back to back (e.g. allocate memory for several of them, determining the size with sizeof). Having the size as 44 forces the alignment in these cases as you requested. However, if the internal offset of each struct member remains the same, you can treat the 44 byte struct as though it was 42 bytes (as long as you take care to align any following data at the correct boundary).

    One trick to try might be putting both of these structs inside a single union type and only use 42-byte version from within each such union.

提交回复
热议问题