结构和对应的可变的尺寸 [英] size of struct and corresponding variable

查看:86
本文介绍了结构和对应的可变的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我定义了一个char变量

If i define a char variable

char a;

和具有单个字符部件的结构

and a structure with a single char member

struct OneChar {
char a;
};

这两个定义将有炭在所有的编译器的大小?
我的疑问是,如果我们的结构定义了一个char变量,由于内存的包装将它带到更多的大小不是char的大小?

Will these both definitions have the size of 'char' in all compilers ? My doubt is, if we define a char variable in structure, due to memory packing will it take more size than size of char ?

推荐答案

这取决于架构和编译器。对于这种特殊的情况下,你应该是安全的,但检查出数据结构填充

It depends on the architecture and the compiler. For this particular case you should be safe, but check out Data Structure Padding.

下面是一个<一个href=\"http://en.wikipedia.org/wiki/Data%5Fstructure%5Falignment#Typical%5Falignment%5Fof%5FC%5Fstructs%5Fon%5Fx86\"相对=nofollow>节选:

在x86 C结构的典型对齐

数据结构成员都存储
  顺序地在一个存储器中,使得在
  所述构件的Data1下的结构
  始终将precede数据2和数据2
  始终将precede数据3:

Data structure members are stored sequentially in a memory so that in the structure below the member Data1 will always precede Data2 and Data2 will always precede Data3:

struct MyData
{
    short Data1;
    short Data2;
    short Data3;
};

如果类型为短被存储在两个
  内存则每个成员字节
  的数据结构之上描绘
  将是2字节字对齐。数据1将
  位于偏移量为0,数据2偏移2
  数据3偏移4.此的大小
  结构将是6个字节。

If the type "short" is stored in two bytes of memory then each member of the data structure depicted above would be 2-byte aligned. Data1 would be at offset 0, Data2 at offset 2 and Data3 at offset 4. The size of this structure would be 6 bytes.

的类型的各部件的
  结构通常具有默认
  对准,这意味着它会,
  除非另有要求
  程序员可以在对准
  pre确定的边界。下列
  典型的路线是有效的
  来自微软,Borland公司,和编译器
  32位x86编译时GNU:

The type of each member of the structure usually has a default alignment, meaning that it will, unless otherwise requested by the programmer, be aligned on a pre-determined boundary. The following typical alignments are valid for compilers from Microsoft, Borland, and GNU when compiling for 32-bit x86:


      
  • 一个char(一个字节)为1字节对齐。

  •   
  • 短(两个字节)将是2字节对齐。

  •   
  • 的int(四字节)将是4字节对齐。

  •   
  • 一个浮点(四字节)将是4字节对齐。

  •   
  • 双(八个字节)将在Windows和Linux上的对齐4字节8字节对齐。

  •   

下面是成员的结构
  各类,共计前8个字节
  编译:

Here is a structure with members of various types, totaling 8 bytes before compilation:

struct MixedData
{
    char Data1;
    short Data2;
    int Data3;
    char Data4;
};


  
  

汇编的数据结构后
  将补充有填充
  字节,以确保适当的对准
  每个成员的:

After compilation the data structure will be supplemented with padding bytes to ensure a proper alignment for each of its members:

struct MixedData  /* after compilation */
{
    char Data1;
    char Padding0[1]; /* For the following 'short' to be aligned on a 2 byte boundary */
    short Data2;
    int Data3;  
    char Data4;
    char Padding1[3];
};


  
  

该结构的编译大小是
  现在是12个字节。要注意的是很重要的
  该最后一个成员是用填充
  所需的字节数
  符合最大类型的
  结构体。在这种情况下,3个字节是
  添加到最后件垫
  结构,以长字的规模。

The compiled size of the structure is now 12 bytes. It is important to note that the last member is padded with the number of bytes required to conform to the largest type of the structure. In this case 3 bytes are added to the last member to pad the structure to the size of a long word.

有可能改变对准
  结构,以减少存储器的
  它们要求(或符合的
  通过改变现有格式)
  编译器的对齐(或包装)
  结构成员。

It is possible to change the alignment of structures to reduce the memory they require (or to conform to an existing format) by changing the compiler’s alignment (or "packing") of structure members.

请求的MixedData
  结构上方对准以一
  字节边界将有编译器
  放弃pre-确定对齐
  成员和没有填充字节
  将插入

Requesting that the MixedData structure above be aligned to a one byte boundary will have the compiler discard the pre-determined alignment of the members and no padding bytes would be inserted.

虽然是没有标准的方法
  限定结构的取向
  成员,一些编译器使用的#pragma
  指令指定的内部包装
  源文件。下面是一个例子:

While there is no standard way of defining the alignment of structure members, some compilers use #pragma directives to specify packing inside source files. Here is an example:

#pragma pack(push)  /* push current alignment to stack */
#pragma pack(1)     /* set alignment to 1 byte boundary */

struct MyPackedData
{
    char Data1;
    long Data2;
    char Data3;
};

#pragma pack(pop)   /* restore original alignment from stack */

此结构将有一个编译
  的6个字节的大小。上述指令
  在由编译器可用
  微软,Borland公司,GNU和许多
  其他。

This structure would have a compiled size of 6 bytes. The above directives are available in compilers from Microsoft, Borland, GNU and many others.

这篇关于结构和对应的可变的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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