Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C “之后的结构”;“打包的”;还是比它应该的大_C_Linux_Gcc_Structure_Memory Alignment - Fatal编程技术网

C “之后的结构”;“打包的”;还是比它应该的大

C “之后的结构”;“打包的”;还是比它应该的大,c,linux,gcc,structure,memory-alignment,C,Linux,Gcc,Structure,Memory Alignment,问题解决了。错误在于没有重新考虑给定的数据类型… 以下结构的尺寸大于其应有尺寸: typedef unsigned char byte; typedef unsigned short word; typedef unsigned long dword; struct Y { short h; byte i; } #if defined (__GNUC__) __attribute__((__packed__)); #endif

问题解决了。错误在于没有重新考虑给定的数据类型… 以下结构的尺寸大于其应有尺寸:

typedef unsigned char    byte;
typedef unsigned short   word;
typedef unsigned long    dword;

struct Y
{
 short      h;
 byte       i;       
}
#if defined (__GNUC__)
    __attribute__((__packed__));
#endif

struct X
{
   short    a;
   byte     b;
   byte     c;
   word     d;
   dword    e;
   byte     f;
   byte     g;
   word     h;
   short    i;
   struct Y G[8];
}
#if defined (__GNUC__)
    __attribute__((__packed__));
#endif

printf("size of X should be 40 but is %i", sizeof(struct X));
输出:

X的大小应为40,但为44

我需要一个40字节大小的结构(所有元素的总和),44是我能达到的最低值。编译器是GCC C,
byte
unsigned char
word
unsigned short
dword
unsigned long
<代码>大小(Y)为3。这里有什么问题?

您定义的类型有缺陷。理想情况下,我会假设
dword
的大小应该是
word
的两倍,但您将两者定义为:

typedef无符号短字;
typedef无符号长双字;
事实证明,在您的平台上,
sizeof(unsigned short)
2
,而
sizeof(unsigned long)
8
,而不是
4

您应该真正避免此类定义,并使用
stdint.h
中提供的标准类型:

byte->uint8\t
短->uint16\t
word->uint16\t
dword->uint32\t
最后,如果未定义宏
\uuu GNUC\uuu
,则结构声明无效,因为您将丢失最后的分号(
)。您可以将其更改为以下内容:

#如果已定义(uu GNUC_uu)
__属性(压缩的)
#恩迪夫
结构
{
uint16_t h;
uint8_t i;
};

您定义的类型有缺陷。理想情况下,我会假设
dword
的大小应该是
word
的两倍,但您将两者定义为:

typedef无符号短字;
typedef无符号长双字;
事实证明,在您的平台上,
sizeof(unsigned short)
2
,而
sizeof(unsigned long)
8
,而不是
4

您应该真正避免此类定义,并使用
stdint.h
中提供的标准类型:

byte->uint8\t
短->uint16\t
word->uint16\t
dword->uint32\t
最后,如果未定义宏
\uuu GNUC\uuu
,则结构声明无效,因为您将丢失最后的分号(
)。您可以将其更改为以下内容:

#如果已定义(uu GNUC_uu)
__属性(压缩的)
#恩迪夫
结构
{
uint16_t h;
uint8_t i;
};

什么是
字节
单词
dword
?这些不是标准类型。此外,您正在打印“X的大小应为”,但您输出的是
Y
的大小。最后,什么是
structj
?请确保您的代码是一个。字节是无符号字符,单词是无符号短的,dword是无符号长的,然后将其添加到问题中的代码中!此外,我们仍然不知道什么是
structj
。你是说结构吗?我还真的怀疑
sizeof(Y)
是否为44。我建议使用
stdint.h
中的类型,而不是依赖于实现的类型,如
short
等,例如,
int16\u t
等。您可以对单个结构字段使用
offsetof
sizeof
来检查与您期望的不同之处。此系统上的
sizeof(dword)
(或
sizeof(无符号长)
)是什么?如果它是8而不是您期望的4,那么这将解释40和44字节之间的差异。什么是
byte
word
dword
?这些不是标准类型。此外,您正在打印“X的大小应为”,但您输出的是
Y
的大小。最后,什么是
structj
?请确保您的代码是一个。字节是无符号字符,单词是无符号短的,dword是无符号长的,然后将其添加到问题中的代码中!此外,我们仍然不知道什么是
structj
。你是说结构吗?我还真的怀疑
sizeof(Y)
是否为44。我建议使用
stdint.h
中的类型,而不是依赖于实现的类型,如
short
等,例如,
int16\u t
等。您可以对单个结构字段使用
offsetof
sizeof
来检查与您期望的不同之处。此系统上的
sizeof(dword)
(或
sizeof(无符号长)
)是什么?如果它是8而不是您期望的4,那么这将解释40和44字节之间的差异。