[不是结构填充和包装的重复。这个问题是关于填充的方式和时间。这是关于如何处理它的信息。]
我刚刚意识到C ++中的对齐会浪费多少内存。考虑以下简单示例:
struct X
{
int a;
double b;
int c;
};
int main()
{
cout << "sizeof(int) = " << sizeof(int) << '\n';
cout << "sizeof(double) = " << sizeof(double) << '\n';
cout << "2 * sizeof(int) + sizeof(double) = " << 2 * sizeof(int) + sizeof(double) << '\n';
cout << "but sizeof(X) = " << sizeof(X) << '\n';
}
Run Code Online (Sandbox Code Playgroud)
使用g ++时,程序将提供以下输出:
sizeof(int) = 4
sizeof(double) = 8
2 * sizeof(int) + sizeof(double) = 16
but …Run Code Online (Sandbox Code Playgroud) c++ optimization memory-alignment memory-layout struct-member-alignment
我是一家新公司,在这家公司中完成了以下结构的使用:
#include <stdio.h>
#include <string.h>
typedef unsigned char uint8;
typedef signed char int8;
typedef unsigned short int uint16;
typedef signed short int int16;
typedef struct padded_t {
int8 array0[11];
int8 array1[4];
uint16 len2;
int8 array2[25];
uint16 len3;
int8 array3[6];
// many more len/arrays follow
} padded_t;
int main(int argc, char** argv)
{
padded_t foo;
memset((void*)&foo, 0, sizeof(padded_t));
int8* str = "foobar";
int16 size = (int16)strlen(str);
int8* ptr = (int8*)&foo.len2;
// please note that the memcpy references only the pointer to …Run Code Online (Sandbox Code Playgroud) 在 msdn for cl 编译器选项 /Zp 上,提到“在 8 字节边界(默认)上打包结构”。这是否意味着如果数据类型的大小 > 8 字节,默认情况下(没有编译器选项或编译指示包)它不会自然对齐,而是具有 8 字节对齐。如果是,GCC/clang 是这种情况吗?
我检查了 GCC,使用-fpack-struct和__uint128_t,它没有任何这样的默认值。例如sizeof(tStruct)是 32 和 24:
struct tStruct {
uint64_t a;
__uint128_t b;
};
Run Code Online (Sandbox Code Playgroud)
我无法在 Windows 上进行检查,因为它不提供任何大于 8 字节的数据类型。
给定以下 c 语言结构体定义:
typedef struct PackTest {
long long a;
int b;
int c;
} PackTest;
Run Code Online (Sandbox Code Playgroud)
Clang-Tidy 给出以下消息:
由于对齐不良,访问结构“PackTest”中的字段效率低下;当前对齐为 8 个字节,但建议对齐为 16 个字节
我知道为什么结构对齐到 8 个字节,但我不知道该建议是否有效以及为什么。
我理解填充是如何工作的.我知道对齐是什么.对我来说奇怪的是,为什么只有char字段的struct的大小不对齐到4个字节(末尾的填充)?我怀疑这不是规范所保证的,所以编译器不这样做.如果是这种情况,我可以参考这样的规则吗?我最感兴趣的是x86和x86-64架构.
例:
struct foo {
char field1;
char field2;
char field3;
} foo2;
int main(void)
{
printf("sizeof=%lu\n", sizeof foo2);
}
Run Code Online (Sandbox Code Playgroud)
输出: sizeof=3
我有一个结构用于构建控制板的消息我需要保持C167 16位Keil编译器和32位Tricore gcc编译器之间的软件兼容性.
typedef struct
{
unsigned char new_weld_status[2];
UINT32 new_weld_count;
UINT16 new_weld_fail_count;
} NEW_PULSE_DATA;
Run Code Online (Sandbox Code Playgroud)
该数组new_weld_status[2]在16位编译器上占用2个字节,在32位编译器上占用4个字节.我正在考虑new_weld_status[2]用gcc编译时替换所有的联合.但是有一个我可以用于gcc的开关,使chars适合/对齐2个字节?
谢谢
我正在尝试为 CAPWAP 协议创建客户端 C 代码。我尝试使用位域结构实现 CAPWAP 标头。但是在使用 sendto() 通过套接字发送这个结构之后,当我使用 wireshark 嗅探数据包时,我发现在它们之间添加了一些额外的位。我不知道这是从哪里来的。请求帮助。提前致谢。

我尝试评论结构的最后几个成员以使其 4 字节对齐。问题仍然存在。
这是原始标题
struct cw_header
{
unsigned preamble : 8;
unsigned hlen : 5;
unsigned rid : 5;
unsigned wbid : 5;
unsigned t : 1;
unsigned f : 1;
unsigned l : 1;
unsigned w : 1;
unsigned m : 1;
unsigned k : 1;
unsigned flags : 3;
unsigned fragment_id : 16;
unsigned fragment_offset : 13;
unsigned reserved : 3;
uint32_t mac_length : 8;
uint32_t mac_addr[6]; …Run Code Online (Sandbox Code Playgroud) c ×5
struct ×3
c++ ×2
gcc ×2
arm9 ×1
bit-fields ×1
c99 ×1
cl ×1
embedded ×1
optimization ×1
visual-c++ ×1
wireshark ×1