#pragma pack effect

后端 未结 11 1696
生来不讨喜
生来不讨喜 2020-11-22 03:19

I was wondering if someone could explain to me what the #pragma pack preprocessor statement does, and more importantly, why one would want to use it.

I

11条回答
  •  轮回少年
    2020-11-22 03:56

    I have seen people use it to make sure that a structure takes a whole cache line to prevent false sharing in a multithreaded context. If you are going to have a large number of objects that are going to be loosely packed by default it could save memory and improve cache performance to pack them tighter, though unaligned memory access will usually slow things down so there might be a downside.

提交回复
热议问题