什么是在C结构的实际大小 [英] What is the actual size of a struct in C

查看:98
本文介绍了什么是在C结构的实际大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  结构的sizeof结果预计不会结果
  结构在内存大小?

下面是code编译Ubuntu服务器11.10上i386的机器:

Here is the code compiled on Ubuntu Server 11.10 for i386 machine:

// sizeof.c
#include <stdio.h>
#include <malloc.h>


int main(int argc, char** argv){
        printf("int's size: %d bytes\n", sizeof(int));
        printf("double's size: %d bytes\n", sizeof(double));
        printf("char's size: %d bytes\n", sizeof(char));
        printf("\n");

        printf("char pointer's size: %d\n", sizeof(char *));
        printf("\n");

        struct Stu{
                int id;
                char* name;
                char grade;
                char sex;
        //      double score;
        };
        printf("struct Stu's pointer's size : %d\n",sizeof(struct Stu *));

        struct Stu stu;
        stu.id=5;
        stu.name="Archer";
        stu.grade='A';
        stu.sex='M';
        printf("Stu(int,char*, char,char)'s size: %d bytes\n", sizeof(struct Stu));
        printf("Stu(5,\"Archer\",'A','M')'s size: %d bytes\n",sizeof(stu));
}

编译:

`gcc -o sizeof sizeof.c`

输出:

int's size: 4 bytes
double's size: 8 bytes
char's size: 1 bytes

char pointer's size: 4

struct Stu's pointer's size : 4
Stu(int,char*, char,char)'s size: 12 bytes
Stu(5,"Archer",'A','M')'s size: 12 bytes

我的问题是,为什么的结构斯图尺寸是12,不是的sizeof(int)的+的sizeof(字符*)+的sizeof(字符) +的sizeof(字符)= 4 + 4 + 1 + 1 = 10,当你把一个双件装入结构斯图的sizeof(结构斯图)`将是20。

My question is why the size of struct Stu is 12, not sizeof(int) + sizeof(char *) + sizeof(char) + sizeof(char) = 4 + 4 + 1 + 1 = 10. When you put a double member intostruct Stu,sizeof(struct Stu)` will be 20.

推荐答案

要计算用户定义类型的大小,在编译器需要考虑所需要的复杂的用户定义的数据结构调整的任何空间。这就是为什么在C结构的尺寸可以比其成员的尺寸的总和。例如,在许多系统上,下面的code将打印8:

To calculate the sizes of user-defined types, the compiler takes into account any alignment space needed for complex user-defined data structures. This is why the size of a structure in C can be greater than the sum of the sizes of its members. For example, on many systems, the following code will print 8:

http://en.wikipedia.org/wiki/Sizeof

假设你有以下结构:

struct A1
{
  char a[2];
  int b;
};

您可能认为的sizeof(A1)等于6,但事实并非如此。它相当于8编译器插入成员'a'和'B'。

You could think that sizeof(A1) equates to 6, but it doesn’t. It equates to 8. The compiler inserts 2 dummy bytes between members ‘a’ and ‘b’.

原因是,编译器将调整成员变量包装大小或类型大小的倍数的倍数,取最小​​的。

The reason is that the compiler will align member variables to a multiple of the pack size or a multiple of the type size, whichever is smallest.

在Visual Studio中的默认包大小为8字节。

The default pack size in visual studio is 8 bytes.

b为整数类型,它是宽4字节。 'B'将被对准到最小的那些2,这是4个字节。如果a为1,2,3或4个字节宽不要紧。 B总是会在相同的地址一致。

‘b’ is of the integer type, which is 4 bytes wide. ‘b’ will be aligned to the minimum of those 2, which is 4 bytes. It doesn’t matter if ‘a’ is 1, 2, 3 or 4 bytes wide. ‘b’ will always be aligned on the same address.

了解更多详情

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

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