结构中的数据成员顺序 [英] Order of data member in a structure

查看:60
本文介绍了结构中的数据成员顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们有以下形式的结构时


typedef struct {

int I1;

int I2;

int I3;

int I4;

浮动F1;

浮动F2;

浮动F3;

浮动F4;

} TMyStru;


和代码段如下;


TMyStru D;

D.I1 = 0; D.I2 = 1; D.I3 = 2; D.I4 = 3;

D.F1 = 4.0; D.F2 = 4.0; D.F3 = 6.0; D.F4 = 7.0;

unsigned char * Buf = new unsigned char [sizeof(TMyStru)];

memcpy(Buf,& D,sizorf(TMyStru)) ;

是否Buf总是(不管特定的C / C ++编译器)包含D的

内容。在I1,I2,I3,I4,F1,F2,F3,F4顺序?

即I1出现在I2前面......

When we have a structure in the following form

typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
float F4;
} TMyStru;

and a code segment as follows;

TMyStru D;
D.I1=0; D.I2=1; D.I3=2; D.I4=3;
D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
unsigned char *Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf, &D, sizorf(TMyStru));
Does "Buf" ALWAYS (regardless of a particular C/C++ compiler) contains the
content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ?
ie I1 appears infront of I2 ...

推荐答案

发布:
当我们有一个以下形式的结构时

typedef struct {
int I1;
int I2;
int I3;
int I4;
浮动F1;
浮动F2;
浮动F3;
浮动F4;
} TMyStru;

和代码段如下;

TMyStru D;
D.I1 = 0; D.I2 = 1; D.I3 = 2; D.I4 = 3;
D.F1 = 4.0; D.F2 = 4.0; D.F3 = 6.0; D.F4 = 7.0;
unsigned char * Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf,& D,sizorf(TMyStru));

是否Buf总是(不管特定的C / C ++
编译器)是否包含D的内容。在I1,I2,I3,I4,F1,F2,F3,F4顺序?
即I1出现在I2的前面......
When we have a structure in the following form

typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
float F4;
} TMyStru;

and a code segment as follows;

TMyStru D;
D.I1=0; D.I2=1; D.I3=2; D.I4=3;
D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
unsigned char *Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf, &D, sizorf(TMyStru));
Does "Buf" ALWAYS (regardless of a particular C/C++ compiler) contains the content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ?
ie I1 appears infront of I2 ...



是的确如此。


只是你知道,你不需要使用堆:


unsigned char Buf [sizeof(TMyStru)];

唯一需要关注的是填充位。在一些

系统中,某些类型的变量需要在内存中的4 b / b
字节边界上对齐。所以对于你的结构,你可以

在内存中有以下内容:


---------

| int |

---------

---------

| int |

---------

---------

| int |

---------

---------

| int |

---------

---------

| padding |

---------

---------

|漂浮|

---------

---------

|漂浮|

---------

---------

|漂浮|

---------

---------

|浮动|

---------


所以当你复制或覆盖填充位时,你就是

访问不属于你的内存。例如,系统

可以将这些位用于其他变量,例如:


TMyStru blah;

char kk [4];


现在系统可能会把kk放到空位:


---------

| int |

---------

---------

| int |

---------

---------

| int |

---------

---------

| int |

---------

---------

| kk |

---------

---------

|漂浮|

---------

---------

|漂浮|

---------

---------

|漂浮|

---------

---------

|漂浮|

---------

我不知道是否有关于何时何地的规则

可以预期填充,但我知道你总是安全的

与:


unsigned char

char < br $>
签名char


及其数组。

-JKop


Yes it does.

Just so you know, you don''t need to use the heap:

unsigned char Buf[sizeof(TMyStru)];
Only thing to be concerned about is padding bits. On some
systems, some types of variables need to be aligned on a 4
byte boundary in memory. So for your structure, you may
have the following in memory:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|padding|
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------

So when you copy or over overwrite the padding bits, you''re
accessing memory that isn''t yours. For instance, the system
may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
I don''t know if there''s any rules as to where and when you
can expect padding, but I do know that your always safe
with:

unsigned char
char
signed char

and arrays of them.
-JKop




" JKop" < NU ** @ NULL.NULL>在消息中写道

新闻:eW ***************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:eW*****************@news.indigo.ie...
发布:
posted:
当我们有一个以下形式的结构时

typedef struct {
int I1;
int I2;
int I3;
int I4;
浮动F1;
浮动F2;
浮动F3;
浮动F4;
} TMyStru;

和代码段如下;

TMyStru D;
D.I1 = 0; D.I2 = 1; D.I3 = 2; D.I4 = 3;
D.F1 = 4.0; D.F2 = 4.0; D.F3 = 6.0; D.F4 = 7.0;
unsigned char * Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf,& D,sizorf(TMyStru));

是否Buf总是(不管特定的C / C ++
When we have a structure in the following form

typedef struct {
int I1;
int I2;
int I3;
int I4;
float F1;
float F2;
float F3;
float F4;
} TMyStru;

and a code segment as follows;

TMyStru D;
D.I1=0; D.I2=1; D.I3=2; D.I4=3;
D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
unsigned char *Buf = new unsigned char [sizeof(TMyStru)];
memcpy(Buf, &D, sizorf(TMyStru));
Does "Buf" ALWAYS (regardless of a particular C/C++


编译器)包含


compiler) contains

D的内容。在I1,I2,I3,I4,F1,F2,F3,F4顺序?
即I1出现在I2的前面......
the content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ?
ie I1 appears infront of I2 ...



是的确如此。

只是你知道,你不需要使用堆:

unsigned char Buf [sizeof(TMyStru)];

关心的是填充位。在某些系统中,某些类型的变量需要在内存中的4字节边界上对齐。因此,对于您的结构,您可能会在记忆中有以下内容:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|填充|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|浮动|
---------

因此,当您复制或覆盖填充位时,您将访问不属于您的内存。例如,系统可以将这些位用于其他变量,如下所示:

TMyStru blah;
char kk [4];

现在系统可以将kk放入空位:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------


Yes it does.

Just so you know, you don''t need to use the heap:

unsigned char Buf[sizeof(TMyStru)];
Only thing to be concerned about is padding bits. On some
systems, some types of variables need to be aligned on a 4
byte boundary in memory. So for your structure, you may
have the following in memory:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|padding|
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------

So when you copy or over overwrite the padding bits, you''re
accessing memory that isn''t yours. For instance, the system
may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------




呀!我希望这不是真的。我知道填充但是第一次

任何人都说编译器可以将填充用于其他目的,而不是仅仅将其闲置。


实际上我很确定它不可能是真的,因为这意味着memset

会影响一个不相关的变量,使得memset在POD类型上无法使用

(除非它们恰好是char或char数组)。如果那确实是真的那么

那么标准就比我想象的要愚蠢。


john



Yikes!! I hope that isn''t true. I know about padding but that the first time
anyone has said that the compiler can use that padding for other purposes,
rather than just leaving it unused.

In fact I''m pretty sure it can''t be true, because it would mean the memset
would affect an unrelated variable, making memset unusable on POD types
(unless they happen to be char or arrays of char). If that really is true
then the standard is stupider than I thought.

john


John Harrison发布:
John Harrison posted:

" JKop" < NU ** @ NULL.NULL>在消息中写道
新闻:eW ***************** @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:eW*****************@news.indigo.ie...
发布:
>当我们有以下形式的结构时
>
> typedef struct {
> int I1;
> int I2;
> int I3;
> int I4;
>漂浮F1;
>浮动F2;
>浮动F3;
>浮动F4;
> } TMyStru;
>
>和代码段如下;
>
> TMyStru D;
> D.I1 = 0; D.I2 = 1; D.I3 = 2; D.I4 = 3;
> D.F1 = 4.0; D.F2 = 4.0; D.F3 = 6.0; D.F4 = 7.0;
> unsigned char * Buf = new unsigned char [sizeof
(TMyStru)]; > memcpy(Buf,& D,sizorf(TMyStru));
>
>
>是否Buf总是(不管特定的C / C ++
编译器)>包含D的内容。在
I1,I2,I3,I4,F1,F2,F3,F4顺序? >即I1出现在I2面前......

是的。

就这样你知道,你不需要使用堆:

unsigned char Buf [sizeof(TMyStru)];

唯一需要关注的是填充位。在某些系统的
上,某些类型的变量需要在内存中的
4字节边界上对齐。因此,对于您的结构,您可能会在记忆中有以下内容:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|填充|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
| float |
---------

所以当你复制或覆盖填充位时,
你正在访问不属于你的内存。例如,
系统可以将这些位用于其他变量,如下所示:

TMyStru blah;
char kk [4];

现在系统可以将kk放入空位:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
---------
|漂浮|
---------
> When we have a structure in the following form
>
> typedef struct {
> int I1;
> int I2;
> int I3;
> int I4;
> float F1;
> float F2;
> float F3;
> float F4;
> } TMyStru;
>
> and a code segment as follows;
>
> TMyStru D;
> D.I1=0; D.I2=1; D.I3=2; D.I4=3;
> D.F1=4.0; D.F2=4.0; D.F3=6.0; D.F4=7.0;
> unsigned char *Buf = new unsigned char [sizeof (TMyStru)]; > memcpy(Buf, &D, sizorf(TMyStru));
>
>
> Does "Buf" ALWAYS (regardless of a particular C/C++ compiler) > contains the content of "D" in I1,I2,I3,I4,F1,F2,F3,F4 order ? > ie I1 appears infront of I2 ...

Yes it does.

Just so you know, you don''t need to use the heap:

unsigned char Buf[sizeof(TMyStru)];
Only thing to be concerned about is padding bits. On some systems, some types of variables need to be aligned on a 4 byte boundary in memory. So for your structure, you may
have the following in memory:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
|padding|
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------

So when you copy or over overwrite the padding bits, you''re accessing memory that isn''t yours. For instance, the system may utilize those bits for some other variable, like so:

TMyStru blah;
char kk[4];

Now the system may stick kk into the vacant spot:

---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| int |
---------
---------
| kk |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------
---------
| float |
---------



哎呀!!我希望这不是真的。我知道填充但



Yikes!! I hope that isn''t true. I know about padding but



,这是第一次有人说编译器可以将
填充用于其他目的,而不是仅仅将其闲置。

实际上我很确定它不可能是真的,因为
意味着memset会影响一个不相关的变量,使得memset
在POD类型上无法使用(除非它们发生是char或
char的数组)。如果确实如此,那么标准就比我想象的那样愚蠢。
约翰


that the first time anyone has said that the compiler can use that padding for other purposes, rather than just leaving it unused.

In fact I''m pretty sure it can''t be true, because it would mean the memset would affect an unrelated variable, making memset unusable on POD types (unless they happen to be char or arrays of char). If that really is true then the standard is stupider than I thought.
john



我得到的是它什么都没有标准

表示*不能*完成,例如在

之后:


struct Cow

{

int a;

char b;

int c;

};


我认为实际上使用b和c之间的空余空间来实现是完全合理的,这可能是

2字节,所以:


int main()

{

Cow blah;


char jack [2];

}


---------

| a |

---------

---------

| b |

---------

---------

| jack [2] |

---------

---------

| c |

---------

C ++包含工会,所以为什么不做以上操作?

-JKop


What I''m getting it is that there''s nothing in the Standard
that says that that *can''t* be done, and for example in the
following:

struct Cow
{
int a;
char b;
int c;
};

I think it would be perfectly reasonable to actually make
use of the vacant space between b and c, which is probably
2 bytes, and so:

int main()
{
Cow blah;

char jack[2];
}

---------
| a |
---------
---------
| b |
---------
---------
|jack[2]|
---------
---------
| c |
---------
C++ contains unions, so why wouldn''t it do the above?
-JKop


这篇关于结构中的数据成员顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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