struct memory padding [英] struct memory padding

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

问题描述

我正在参加C ++的入门课程,而我们的老师并没有确定如何确定内存填充的结果。


如果结构内定义的所有组件使用的内存

介于某个渐变之间,结构向上舍入。到了最接近梯度倍数的



这位老师有些不稳定,并且在随后的几天里说过

padding四舍五入到4的最接近的倍数,

然后是最接近的2的幂。


我自己阅读,它看起来像填充结构是

由平台的字大小决定,我假设这意味着

的倍数为16.


可能有人精通C ++,请指出这是怎么回事?$ / b

干杯,

西蒙


-

巨大的故事
www。 stupendoustales.com

投机小说,纸浆梦想

I''m taking an intro course on C++, and our teacher is not being
clear on how stuct memory padding is determined.

If the memory used by all components defined inside a struct
falls between a certain gradient, a struct "rounds up" to the
nearest multiple of the gradient.

This teacher is somewhat erratic and has said on subsequent days
that first the padding is rounded up to nearest multiple of 4,
and then the nearest power of 2.

I reading on my own, it looks like the padding of the struct is
determined by the word size fo the platform, which I assume means
multiples of 16.

Could someone well-versed in C++ please point out how this is
done?

Cheers,
Simon

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams

推荐答案

si **** @ nospam.com 写道:

我正在参加C ++的入门课程,还有我们的茶如果确定stuct内存填充是如何确定的话,那就是明确内存填充的结果。

如果在结构中定义的所有组件使用的内存

落在某个梯度,结构四舍五入之间。到了最接近梯度倍数的



这位老师有些不稳定,并且在随后的几天里说过

padding四舍五入到4的最接近的倍数,

然后是最接近的2的幂。


我自己阅读,它看起来像填充结构是

由平台的字大小决定,我假设这意味着

的倍数为16.


可能有人精通C ++,请指出这是如何完成的?
I''m taking an intro course on C++, and our teacher is not being
clear on how stuct memory padding is determined.

If the memory used by all components defined inside a struct
falls between a certain gradient, a struct "rounds up" to the
nearest multiple of the gradient.

This teacher is somewhat erratic and has said on subsequent days
that first the padding is rounded up to nearest multiple of 4,
and then the nearest power of 2.

I reading on my own, it looks like the padding of the struct is
determined by the word size fo the platform, which I assume means
multiples of 16.

Could someone well-versed in C++ please point out how this is
done?



虽然没有标准定义,但填充往往是自然

处理器对齐所以在32位处理器上,结构


struct X {

char a;

int b;

};


的大小为8,在a后有三个填充字节。 />

有些平台支持打包以消除填充,但这是非标准的b
$ b在某些处理器上。


-

Ian Collins。

While not defined by the standard, padding tends to be to the natural
alignment of the processor So on a 32 bit processor, the struct

struct X {
char a;
int b;
};

will have a size of 8 with three padding bytes after ''a''.

Some platforms support packing to eliminate padding, but this is
non-standard and impossible on some processors.

--
Ian Collins.


希望这可以帮助你/> http://www.thescripts.com/forum/thread543879.html
si****@nospam.com 写道:
Hope this helps you
http://www.thescripts.com/forum/thread543879.html
si****@nospam.com wrote:

我正在学习C ++的入门课程,我们的老师并没有确定如何确定存储器填充的结果。


如果是内存由结构内定义的所有组件使用

落在某个渐变之间,结构向上舍入。到了最接近梯度倍数的



这位老师有些不稳定,并且在随后的几天里说过

padding四舍五入到4的最接近的倍数,

然后是最接近的2的幂。


我自己阅读,它看起来像填充结构是

由平台的字大小决定,我假设这意味着

的倍数为16.


可能有人精通C ++,请指出这是怎么回事?$ / b

干杯,

西蒙


-

巨大的故事
www。 stupendoustales.com

投机小说,纸浆梦想
I''m taking an intro course on C++, and our teacher is not being
clear on how stuct memory padding is determined.

If the memory used by all components defined inside a struct
falls between a certain gradient, a struct "rounds up" to the
nearest multiple of the gradient.

This teacher is somewhat erratic and has said on subsequent days
that first the padding is rounded up to nearest multiple of 4,
and then the nearest power of 2.

I reading on my own, it looks like the padding of the struct is
determined by the word size fo the platform, which I assume means
multiples of 16.

Could someone well-versed in C++ please point out how this is
done?

Cheers,
Simon

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams


非常感谢。结构的填充将使其达到单词大小的下一个倍数,例如。 32bit / 4byte


干杯,

西蒙


-

惊人的故事
www.stupendoustales.com

投机小说,纸浆梦想
Thanks so much. Padding of the struct will bring it up to the
next multiple of the word size, eg. 32bit/4byte

Cheers,
Simon

--
Stupendous Tales
www.stupendoustales.com
Speculative Fiction, Pulp Dreams


这篇关于struct memory padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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