为什么这两个结构的大小是不同的? [英] Why the size of these two structs are different?

查看:147
本文介绍了为什么这两个结构的大小是不同的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member\">Why为等于每个成员的sizeof的总和一个结构是不是和sizeof?


 #包括LT&;&stdio.h中GT;结构CSIE {
  焦炭℃;
  短S;
  INT I;
  双层电子;
};结构CEIS {
  焦炭℃;
  双层电子;
  INT I;
  短S;
};诠释主要(无效){
  的printf(CSIE =%d个\\ N的sizeof(结构CSIE));
  的printf(CEIS =%d个\\ N的sizeof(结构CEIS));
  返回0;
}

输出是:

CSIE = 16

CEIS = 24


解决方案

这是非常依赖于体系结构,并且没有指定你在什么类型的系统。

不过,假设


  • 字符:1字节大小,没有对齐

  • :2字节大小,对齐到2字节边界

  • INT :4字节大小,对齐到4字节边界

  • 双击:8字节大小,对齐到8字节边界

这是很容易解释。


+ ------ + + ------ +
|字符| 0 |字符|
+ ------ + + ------ +
| | 1 | |
+ ------ + | |
| | 2 | |
|短| | |
| | 3 | |
+ ------ + | |
| | 4 | |
| | | |
| | 5 | |
| INT | | |
| | 6 | |
| | | |
| | 7 | |
+ ------ + + ------ +
| | 8 | |
| | | |
| | 9 | |
| | | |
| | 10 | |
| | | |
| | 11 | |
|双| |双|
| | 12 | |
| | | |
| | 13 | |
| | | |
| | 14 | |
| | | |
| | 15 | |
+ ------ + + ------ +
         16 | |
            | |
         17 | |
            | INT |
         18 | |
            | |
         19 | |
            + ------ +
         20 | |
            |短|
         21 | |
            + ------ +
         22 | |
            | |
         23 | |
            + ------ +

有在你的结构空(称为填充),因为某些数据结构必须落在特定字节边界。

请注意,该结构作为一个整体必须对齐到8字节边界,以保持其成员的一致;这就是为什么有上的第二个版本尾部微胖。

Possible Duplicate:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?

#include<stdio.h>

struct csie {
  char c;
  short s;
  int i;
  double e;
};  

struct ceis {
  char c;
  double e;
  int i;
  short s;
};

int main(void) {
  printf("csie = %d\n", sizeof(struct csie));
  printf("ceis = %d\n", sizeof(struct ceis));
  return 0;
}

Output is:

csie = 16

ceis = 24

解决方案

This is very architecture dependent, and you don't specify what type of system you're on.

However, assuming

  • char: 1 byte size, no alignment
  • short: 2 byte size, aligned to 2 byte boundaries
  • int: 4 byte size, aligned to 4 byte boundaries
  • double: 8 byte size, aligned to 8 byte boundaries

this is easily explained.

+------+    +------+
| char |  0 | char |
+------+    +------+
|      |  1 |      |
+------+    |      |
|      |  2 |      |
| short|    |      |
|      |  3 |      |
+------+    |      |
|      |  4 |      |
|      |    |      |
|      |  5 |      |
|  int |    |      |
|      |  6 |      |
|      |    |      |
|      |  7 |      |
+------+    +------+
|      |  8 |      |
|      |    |      |
|      |  9 |      |
|      |    |      |
|      | 10 |      |
|      |    |      |
|      | 11 |      |
|double|    |double|
|      | 12 |      |
|      |    |      |
|      | 13 |      |
|      |    |      |
|      | 14 |      |
|      |    |      |
|      | 15 |      |
+------+    +------+
         16 |      |
            |      |
         17 |      |
            |  int |
         18 |      |
            |      |
         19 |      |
            +------+
         20 |      |
            | short|
         21 |      |
            +------+
         22 |      |
            |      |
         23 |      |
            +------+

There is empty space (called padding) in your structures, because certain data structures must fall on certain byte boundaries.

Note that the struct as a whole must be aligned to 8 byte boundaries to maintain the alignment of its members; that's why there's extra padding on the tail of the second version.

这篇关于为什么这两个结构的大小是不同的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆