关于__attribute__ ((packed))
生活随笔
收集整理的這篇文章主要介紹了
关于__attribute__ ((packed))
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>using namespace std;typedef struct
{unsigned char ver;unsigned char cmd;unsigned short id;unsigned short retcode;
}__attribute__ ((packed)) ACKPACK_CMD;struct my
{char ch; int a;
}__attribute__ ((packed)); // 取消結構在編譯過程中的優化對齊,按照實際占用字節數進行對齊struct my2
{char ch; int a;
}; // 默認的字節對齊方式(4字節對齊, 不同編譯器可能不同)int main()
{cout << sizeof(ACKPACK_CMD) << endl;cout << sizeof(my) << endl;cout << sizeof(my2) << endl;return 0;
}/*結論:1. 在win xp上運行結果為: 6 5 8, 編譯器為g++(cl.exe根本不認識packed)1. 在win7上運行結果為: 6 8 8, 編譯器為g++2. 在ubuntu上運行結果為: 6 5 8, 編譯器為g++說明:__attribute__ ((packed))的作用就是告訴編譯器取消結構在編譯過程中的優化對齊,按照實際占用字節數進行對齊,是GCC特有的語法。這個功能是跟操作系統沒關系,跟編譯器有關.
*/
總結
以上是生活随笔為你收集整理的关于__attribute__ ((packed))的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用CMD命令修改Windows本地账户
- 下一篇: 数组实现矩阵逐层向内层加1