Memory alignment : how to use alignof / alignas?

后端 未结 4 537
星月不相逢
星月不相逢 2020-12-02 05:43

I work with shared memory right now.

I can\'t understand alignof and alignas.

cppreference is unclear : alignof return

4条回答
  •  一生所求
    2020-12-02 06:20

    Each type has an alignment requirement. Generally, this is so variables of the type can be accessed efficiently, without having to cause the CPU to generate more than one read/write access in order to reach any given member of the datatype. Furthermore, it also ensure efficient copying of the entire variable. alignof will return the alignment requirement for the given type.

    alignas is used to force an alignment on a datatype (so long as it is not less stringent that what alignof said datatype would return)

提交回复
热议问题