填充字节的波动性? [英] Volatility of padding bytes?

查看:54
本文介绍了填充字节的波动性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情况。我有一个大的静态数组s []
结构,比如大小为500.我还需要一个较小的字符数组,比如

size 100,什么都没有与结构有关。


为了节省内存,我想尽可能使用(在检查后用

offsetof和sizeof表示''足够的填充以适合字符 -

通常它将是结构中字段之间的空间来存储这些

字符。如果填充位于字段sa和sb之间,那么我可以将我的第n个字符放在((char *)& s [n] .a)+ sizeof(s [n] .a。 )。


为了这个工作,我需要知道:

1)更改填充字节不会影响实现方式

解释结构

2)当

操纵结构时,实现不会覆盖填充字节

这些东西有保障吗?

Consider the following situation. I have a large static array s[] of
structs, say of size 500. I also need a smaller array of chars, say of
size 100, which has nothing to do with the struct.

To conserve memory, I''d like if possible to use (after checking with
offsetof and sizeof that there''s enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I''d need to know:
1) changing the padding bytes won''t affect how the implementation
interprets the struct
2) the implementation won''t overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?

推荐答案

Fr ************ @ googlemail.com écrit:
Fr************@googlemail.com a écrit :

考虑以下情况。我有一个大的静态数组s []
结构,比如大小为500.我还需要一个较小的字符数组,比如

size 100,什么都没有与结构有关。


为了节省内存,我想尽可能使用(在检查后用

offsetof和sizeof表示''足够的填充以适合字符 -

通常它将是结构中字段之间的空间来存储这些

字符。如果填充位于字段sa和sb之间,那么我可以将我的第n个字符放在((char *)& s [n] .a)+ sizeof(s [n] .a。 )。


为了这个工作,我需要知道:

1)更改填充字节不会影响实现方式

解释结构

2)当

操纵结构时,实现不会覆盖填充字节

这些东西是否得到保证?
Consider the following situation. I have a large static array s[] of
structs, say of size 500. I also need a smaller array of chars, say of
size 100, which has nothing to do with the struct.

To conserve memory, I''d like if possible to use (after checking with
offsetof and sizeof that there''s enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I''d need to know:
1) changing the padding bytes won''t affect how the implementation
interprets the struct
2) the implementation won''t overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?


Fr************@googlemail.com 写道:

>请考虑以下情况。我有一个大型静态数组s []的结构,比如大小为500.我还需要一个较小的字符数组,比如说大小为100,这与结构无关。为了节省内存,我想在可能的情况下使用(在使用
offsetof和sizeof检查后,有足够的填充符合字符 -
通常它会是结构中的字段之间的空间来存储这些字符。如果填充在字段sa和sb之间,那么我可以将我的第n个字符放在((char *)& s [n] .a)+ sizeof(s [n] .a)。 1)更改填充字节不会影响实现如何解释结构
2)当
操作结构时,实现不会覆盖填充字节
这些东西是否得到保证?
>Consider the following situation. I have a large static array s[] of
structs, say of size 500. I also need a smaller array of chars, say of
size 100, which has nothing to do with the struct.

To conserve memory, I''d like if possible to use (after checking with
offsetof and sizeof that there''s enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I''d need to know:
1) changing the padding bytes won''t affect how the implementation
interprets the struct
2) the implementation won''t overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?



(2)不是。例如,可以在内部使用memcpy()来实现结构分配。


而不是使代码难以理解且难以理解,为什么不呢? br />
使填充字节''可见''


例如,如果您的原始结构是


struct s {

char c; / *假设8位* /

/ * 1字节填充* /

短s; / *假设16位* /

/ *这里填充两个字节* /

long l; / *假设32位* /

};


(对于许多32位架构而言是典型的)


重写它为:


struct s_new_and_improved {

char c;

char padding1;

short s ;

char padding [2];

long l;

};


更好,如果你可以重新安排字段:


struct s_latest_and_greatest {

long l;

short s;

char c;

char padding [3];

};

Roberto Waltman


[请回复小组,

返回地址无效]

(2) Is not. A structure assignment could be implemented, for example,
using memcpy() internally.

Instead of making your code unreadable and hard to understand, why not
make the padding bytes ''visible'' ?

For example, if your original struct was

struct s {
char c; /* assume 8 bits */
/* 1 byte padding here */
short s; /* assume 16 bits */
/* two bytes padding here */
long l; /* assume 32 bits */
};

(Typical for many 32 bit architectures)

rewrite it as:

struct s_new_and_improved {
char c;
char padding1;
short s;
char padding[2];
long l;
};

better yet, if you can rearrange the fields:

struct s_latest_and_greatest {
long l;
short s;
char c;
char padding[3];
};
Roberto Waltman

[ Please reply to the group,
return address is invalid ]


Fr ************ @ googlemail.com 写道:

考虑以下情况。我有一个大的静态数组s []
结构,比如大小为500.我还需要一个较小的字符数组,比如

size 100,什么都没有与结构有关。


为了节省内存,我想尽可能使用(在检查后用

offsetof和sizeof表示''足够的填充以适合字符 -

通常它将是结构中字段之间的空间来存储这些

字符。如果填充位于字段sa和sb之间,那么我可以将我的第n个字符放在((char *)& s [n] .a)+ sizeof(s [n] .a。 )。


为了这个工作,我需要知道:

1)更改填充字节不会影响实现方式

解释结构

2)当

操纵结构时,实现不会覆盖填充字节

这些东西有保证吗?
Consider the following situation. I have a large static array s[] of
structs, say of size 500. I also need a smaller array of chars, say of
size 100, which has nothing to do with the struct.

To conserve memory, I''d like if possible to use (after checking with
offsetof and sizeof that there''s enough padding to fit a char -
usually it will be) space between fields in the structs to store these
chars. If the padding is between fields s.a and s.b then I could take
my nth char to be at ((char *) &s[n].a)+sizeof(s[n].a).

For this to work, I''d need to know:
1) changing the padding bytes won''t affect how the implementation
interprets the struct
2) the implementation won''t overwrite padding bytes itself when
manipulating the struct
Are these things guaranteed?



为什么不重新排序结构以消除填充?


-

Ian Collins。

Why don''t you reorder the struct to eliminate the padding?

--
Ian Collins.


这篇关于填充字节的波动性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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