比较两个结构的大小 [英] comparing sizes of two structures

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

问题描述

假设有五种DISTINCT类型TYPE1,TYPE2,TYPE3,TYPE4,

TYPE5,数据。


考虑结构:


struct struct_one

{

TYPE1 data1;

TYPE2 data2;

TYPE3 data3;

TYPE4 data4;

TYPE5 data5;

} var_one;


struct struct_two

{

TYPE2 data1;

TYPE5 data2;

TYPE1 data3;

TYPE3 data4;

TYPE4 data5;

} var_two;


var_one和var_two的大小如何比较 - 它们是否为

相同或不同?

解决方案

su ************** @ yahoo.com ,印度写道:


假设有五种DISTINCT类型TYPE1,TYPE2,TYPE3,TYPE4,

TYPE5,数据。


考虑结构:


struct st ruct_one

{

TYPE1 data1;

TYPE2 data2;

TYPE3 data3;

TYPE4 data4;

TYPE5 data5;

} var_one;


struct struct_two

{

TYPE2 data1;

TYPE5 data2;

TYPE1 data3;

TYPE3 data4;

TYPE4 data5;

} var_two;


var_one和var_two的大小如何比较 - 它们是否相同

或者不同?



要么,这取决于类型和实现。


-

Ian Collins。


su * *************@yahoo.com ,印度写道:


假设有五种DISTINCT类型TYPE1,TYPE2,TYPE3,TYPE4,

TYPE5,数据。


考虑结构:


struct struct_one

{

TYPE1 data1;

TYPE2 data2;

TYPE3 data3;

TYPE4 data4;

TYPE5 data5;

} var_one;


struct struct_two

{

TYPE2 data1;

TYPE5 data2;

TYPE1 data3;

TYPE3 data4;

TYPE4数据5;

} var_two;


var_one和var_two的大小如何比较 - 它们将是

相同或不同?



这取决于您的类型的编译器和特征(主要是对齐

要求)。


2007年3月7日00:16:50 -0800,su ************** @ yahoo.com,India

< su ************** @ yahoo.comwrote:


>假设有五个DISTINCT类型TYPE1,TYPE2,TYPE3,TYPE4,
TYPE5,数据。

考虑结构:

struct struct_one

{

TYPE1 data1;

TYPE2 data2;

TYPE3 data3;

TYPE4 data4;

TYPE5 data5;
} var_one;

struct struct_two

{

TYPE2 data1;

TYPE5数据2;

TYPE1数据3;

TYPE3数据4;

TYPE4 data5;
} var_two;

var_one和var_two的大小如何比较 - 它们是相同还是不同?



它们可以是相同的,也可以是不同的。它取决于

确切类型的TYPE *和编译器。编译器可以在

命令中添加填充以对齐其自然边界上的字段,并且没有

保证一个编译器将字段打包为另一个

编译器,特别是当编译器针对不同的平台时。


尝试编译并运行以下代码:


#include< stddef .h>

#include< stdio.h>


typedef char TYPE1;

typedef double TYPE2;

typedef int TYPE3;

typedef short TYPE4;

typedef long TYPE5;


struct s1

{

TYPE1 d1;

TYPE2 d2;

TYPE3 d3;

TYPE4 d4;

TYPE5 d5;

} var_one;


struct s2

{

TYPE2 d1;

TYPE5 d2;

TYPE1 d3;

TYPE3 d4;

TYPE4 d5;

} var_two;


int main(无效)

{

printf(" s1偏移:\\\
");

printf(" offsetof(d1)=%d \ n",(int)offsetof(struct s1,d1));

printf( " offsetof(d2)=%d \ n",(int)offsetof(struct s1,d2));

printf(" offsetof(d3)=%d \ n", (int)offsetof(struct s1,d3));

printf(" offsetof(d4)=%d \ n",(int)offsetof(struct s1,d4));

printf(" offsetof(d5)=%d \ n",(int)offsetof(struct s1,d5));


printf(" \ n");

printf(" s2 offsets:\ n");

printf(" offsetof(d1)=%d \ n" ,(int)offsetof(struct s2,d1));

printf(" offsetof(d2)=%d \ n",(int)offsetof(struct s2,d2));

printf(" offsetof(d3)=%d \ n",(int)offsetof(struct s2,d3));

printf(" offsetof(d4) )=%d \ n",(int)offsetof(struct s2,d4));

printf(" offsetof(d5)=%d \ n",(int)offsetof( struct s2,d5));


返回n 0;

}


我符合C的实施输出:


s1抵消:

offsetof(d1)= 0

offsetof(d2)= 8

offsetof(d3)= 16

offsetof(d4)= 20

offsetof(d5)= 24

s2抵消:

offsetof(d1)= 0

offsetof(d2)= 8

offsetof(d3)= 12

offsetof(d4)= 16

offsetof(d5)= 20

按任意键继续


问候

-

jay


Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?

解决方案

su**************@yahoo.com, India wrote:

Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?

Either, it depends on the types and the implementation.

--
Ian Collins.


su**************@yahoo.com, India wrote:

Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?

It depends on the compiler and the characteristics, (mainly alignment
requirements), of your types.


On 7 Mar 2007 00:16:50 -0800, "su**************@yahoo.com, India"
<su**************@yahoo.comwrote:

>Suppose there are five DISTINCT types TYPE1, TYPE2, TYPE3, TYPE4,
TYPE5, of data.

Consider the structures:

struct struct_one
{
TYPE1 data1;
TYPE2 data2;
TYPE3 data3;
TYPE4 data4;
TYPE5 data5;
} var_one;

struct struct_two
{
TYPE2 data1;
TYPE5 data2;
TYPE1 data3;
TYPE3 data4;
TYPE4 data5;
} var_two;

How do the sizes of var_one and var_two compare - will they be the
same or different ?

They could be the same or they could be different. It depends on the
exact types of TYPE* and on the compiler. Compilers can add padding in
order to align fields on their natural boundaries, and there is no
guarantee that one compiler will pack fields the same as another
compiler, especially so when the compilers target different platforms.

Try compiling and running the following:

#include <stddef.h>
#include <stdio.h>

typedef char TYPE1;
typedef double TYPE2;
typedef int TYPE3;
typedef short TYPE4;
typedef long TYPE5;

struct s1
{
TYPE1 d1;
TYPE2 d2;
TYPE3 d3;
TYPE4 d4;
TYPE5 d5;
} var_one;

struct s2
{
TYPE2 d1;
TYPE5 d2;
TYPE1 d3;
TYPE3 d4;
TYPE4 d5;
} var_two;

int main(void)
{
printf("s1 offsets:\n");
printf("offsetof(d1) = %d\n", (int)offsetof(struct s1, d1));
printf("offsetof(d2) = %d\n", (int)offsetof(struct s1, d2));
printf("offsetof(d3) = %d\n", (int)offsetof(struct s1, d3));
printf("offsetof(d4) = %d\n", (int)offsetof(struct s1, d4));
printf("offsetof(d5) = %d\n", (int)offsetof(struct s1, d5));

printf("\n");
printf("s2 offsets:\n");
printf("offsetof(d1) = %d\n", (int)offsetof(struct s2, d1));
printf("offsetof(d2) = %d\n", (int)offsetof(struct s2, d2));
printf("offsetof(d3) = %d\n", (int)offsetof(struct s2, d3));
printf("offsetof(d4) = %d\n", (int)offsetof(struct s2, d4));
printf("offsetof(d5) = %d\n", (int)offsetof(struct s2, d5));

return 0;
}

My conforming C implementation outputs:

s1 offsets:
offsetof(d1) = 0
offsetof(d2) = 8
offsetof(d3) = 16
offsetof(d4) = 20
offsetof(d5) = 24

s2 offsets:
offsetof(d1) = 0
offsetof(d2) = 8
offsetof(d3) = 12
offsetof(d4) = 16
offsetof(d5) = 20
Press any key to continue

Regards
--
jay


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

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