简体   繁体   English

#pragma pack()调用有什么作用?

[英]What does #pragma pack () call do?

I was given the following structs: 我得到了以下结构:

struct s2 {
int f0;
char f1;
unsigned long long f2;
unsigned long f3;
short f4;
long f5;
};

#pragma pack (1)
struct s2_packed {
int f0;
char f1;
unsigned long long f2;
unsigned long f3;
short f4;
long f5;
};
#pragma pack ()

struct s3 {
unsigned short f0;
int f1;
unsigned int f2;
int f3;
unsigned short f4;
char f5;
};

I understand s2_packed should be packed now, but is s3 packed? 我知道s2_packed现在应该打包,但是s3打包? What does the call #pragma pack () do? #pragma pack ()是什么?

UPDATE UPDATE

I just examined the bytes in s3 and they are indeed packed.. Not sure whats going on here, any ideas? 我只是检查了s3中的字节,它们确实已经打包了。

UPDATE UPDATE

Here is my makefile I was given: 这是我得到的我的makefile

all: 
 gcc -m32 -g -O -Wall struct.c main.c -o struct

clang:
 clang -m32 -g -O -Wall struct.c main.c -o struct

strict:
 gcc -m32 -g -O -Wall -Werror struct.c main.c -o struct

clang-strict:
 clang -m32 -g -fsanitize=undefined -O -Wall -Werror struct.c main.c -o struct

clean:
 rm -f struct
#pragma pack ()

with gcc and some other compilers restores the default packing of the implementation. gcc和其他一些编译器一起使用可恢复实现的默认打包。

Documentation for gcc of this #pragma is here: http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html #pragma gcc文档在此处: http : //gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM