当前位置: 动力学知识库 > 问答 > 编程问答 >

c - A __attribute__((packed)) like attribute not GCC Specific

问题描述:

I use __attribute__((packed)); to make items of a struct being stored in memory after another as this is critical for some low-level development.

As __attribute__((packed)); is GCC specific I wonder if there is a similar solution that works on ALL ANSI/C89/C99/C11 compilers or at least some of them.

网友答案:

There is no standard approach to accomplish what __attribute__((packed)) does. The typical solution is to use #ifdef's to handle different compilers. You can find a few solutions to this approach at this SO post which also contains the details on the Visual C++ equivalent of __attribute__((packed)). Alternatively, GCC supports the Windows struct packing pragmas, so if you are just concerned with Windows and GCC you could just use the Windows approach.

网友答案:

There is no support for features to control struct layout specified by the standard. The standard simply states that this aspect is implementation defined.

Therefore, if you do need to control layout, you will need to use compiler specific functionality. If you can find a way to avoid needing to do this at all, that would be preferable.

分享给朋友:
您可能感兴趣的文章:
随机阅读: