struct PackedData {
unsigned int : 3; // 3 位元填充
unsigned int f1 : 1;
unsigned int f2 : 2;
unsigned int f3 : 1;
unsigned int f4 : 1;
};
int main(void){
printf("size: %zu\n", sizeof(sturct PackedData));
return 0;
}
會返回4Bytes,推測為編譯器在編譯時還是會以unsigned int
為基底,如果想要讓編譯器強制根據Bit-padding佈局,可以使用以下幾種方法
struct __attribute__((packed)) PackedData {
unsigned int : 3; // 3 位元填充
unsigned int f1 : 1;
unsigned int f2 : 2;
unsigned int f3 : 1;
unsigned int f4 : 1;
};
#pragma pack(1)
struct PackedData {
unsigned int : 3;
unsigned int f1 : 1;
unsigned int f2 : 2;
unsigned int f3 : 1;
unsigned int f4 : 1;
};
#pragma pack()
struct PackedData {
unsigned char : 3; // 3 位元填充
unsigned char f1 : 1;
unsigned char f2 : 2;
unsigned char f3 : 1;
unsigned char f4 : 1;
};
都會返回PackedData為1Byte
另外更改Bit padding來符合memory alignment如果存入超過指定大小的data也是會發生overflow的,需要特別注意
int main() {
struct PackedData data = {0};
data.f1 = 2; // Overflow!
printf("f1: %u\n", data.f1); // 查看溢位後的結果
return 0;
}
sys.modules is a dictionary mapping the names of imported modules to the module object holding the code
Apr 28, 2025Date: 2017-01-20 11:54
Apr 22, 2025** Warnings**
Apr 22, 2025qemu-aarch -g [port] [debug_file]gdb-multiarch [debug_file](gdb) target remote localhost:1234
Apr 17, 2025or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up