我有這樣的示例程序低於上述程序的的#pragma pack()的與推入和彈出VS解壓
#include <stdio.h>
#include <stdlib.h>
#pragma pack(push)
#pragma pack(1)
typedef struct{
char a;
int b;
char c;
}st_a;
#pragma pack(pop)
typedef struct{
char a;
int b;
char c;
}st_b;
int main()
{
printf("size of struct a %zd \n",sizeof(st_a));
printf("size of struct b %zd \n",sizeof(st_b));
return 0;
}
輸出是
size of struct a 6
size of struct b 12
現在,如果更改,如下所述結構聲明:該方案的
#pragma pack(1)
typedef struct{
char a;
int b;
char c;
}st_a;
#pragma unpack()
輸出是
size of struct a 6
size of struct b 6
爲什麼這種行爲差異?我的理解是,兩個結構聲明都在做同樣的事情。
我在MBP上運行這個。
$gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix
您正在使用哪種編譯器? – slugonamission
我猜你正在使用MSVC。 AFAICT沒有編譯'unpack()' – Hasturkun
@Hasturkun - 儘管瑞薩編譯器支持'#pragma unpack',但似乎不支持'#pragma pack(push)':S。 – slugonamission