“新”不考虑结构填充 [英] structure padding not considered by 'new'

查看:76
本文介绍了“新”不考虑结构填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我发现在使用''new''运算符时,结构填充没有被考虑到



有没有办法启用它?


struct Dataunit

{

char dataid [20];

int keyid;

浮动结果;

} * p2;


p2 = new Dataunit; //这是分配26个字节。


这个分配26个字节。奇怪。 :( :(。

C ++跳过结构填充概念,同时分配内存

使用''new''?


Thx in advans,

Karthik Balaguru

解决方案

karthikbalaguru写道:


我发现使用''new''运算符时没有考虑结构填充



有没有办法启用它?


struct Dataunit

{

char dataid [20];

int keyid;

浮动结果;

} * p2;


p2 = new Dataunit; //这是分配26个字节。


这个分配26个字节。奇怪。:( :(。



为什么这很奇怪?在sizeof的系统上(int)== 2那就是我想要的
期待......


C ++是否跳过了结构填充概念同时分配内存

使用''new''?



好​​吧,你发布的代码不能编译。请发布一个可编辑的

示例。填充是特定于实现的。请至少命名您正在使用的

编译器。你如何确定''new''

分配的字节数和你期望的是什么,以及为什么它们不同(如果它们b
不同)?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


在2007-09-04 10:43:47 -0400,karthikbalaguru

< ka *************** @ gmail.comsaid:





我发现在使用''new''运算符时没有考虑结构填充



有没有办法启用它?


struct Dataunit

{

char dataid [20];

int keyid;

浮动结果;

} * p2;


p2 =新的Dataunit; //这是分配26个字节。


这个分配26个字节。奇怪。 :( :(。

C ++在分配内存时跳过结构填充概念

使用''new''?



结构填充发生在数组中.new没有分配数组,因此它可以处理未填充类型的大小。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:一个教程和参考的作者
www.petebecker.com/tr1book


< blockquote> 9月4日晚上8:24,Pete Becker< p ... @ versatilecoding.comwrote:


2007-09-04 10:43: 47 -0400,karthikbalaguru

< karthikbalagur ... @ gmail.comsaid:





我发现使用''new''运算符时没有考虑结构填充



有没有办法启用它?


struct Dataunit

{

char dataid [20];

int keyid;

浮动结果;

} * p2;


p2 = new Dataunit; //这是分配26个字节。


这会分配26个字节。奇怪。 :( :(。

C ++跳过结构填充概念,同时分配内存

使用''new''?


结构填充发生在数组中.new不分配数组,因此它可以处理未填充类型的大小。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com

标准C ++库扩展:一个教程和参考的作者
www.petebecker.com/tr1book) - 隐藏引用的文字 -


- 显示引用文本 -



sizeof(struct Dataunit)的输出肯定会有所不同

它会考虑到<基于体系结构(处理器)的
填充。

''new''不将结构填充变为ac计算尺寸

计算分配内存?


Thx in advans,

Karthik Balaguru


Hi,

I find that the structure padding is not being taken into account
while using ''new'' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using ''new'' ?

Thx in advans,
Karthik Balaguru

解决方案

karthikbalaguru wrote:

I find that the structure padding is not being taken into account
while using ''new'' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.

Why is it strange? On a system where sizeof(int) == 2 that''s what I''d
expect...

Does C++ skip the Structure Padding concept while allocation of memory
using ''new'' ?

Well, the code you posted does not compile. Please post a compilable
example. Padding is implementation-specific. Please at least name the
compiler you''re using. How do you determine how many bytes the ''new''
allocates and what did you expect, and why are they different (if they
are different)?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On 2007-09-04 10:43:47 -0400, karthikbalaguru
<ka***************@gmail.comsaid:

Hi,

I find that the structure padding is not being taken into account
while using ''new'' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using ''new'' ?

Structure padding occurs in arrays. new doesn''t allocate arrays, so it
can deal in the unpadded size of the type.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


On Sep 4, 8:24 pm, Pete Becker <p...@versatilecoding.comwrote:

On 2007-09-04 10:43:47 -0400, karthikbalaguru
<karthikbalagur...@gmail.comsaid:


Hi,

I find that the structure padding is not being taken into account
while using ''new'' operator.
Is there a way to enable it ?

struct Dataunit
{
char dataid[20];
int keyid;
float result;
} *p2;

p2 = new Dataunit; // This is allocating 26 bytes.

This allocates 26 bytes. Strange . :(:(.
Does C++ skip the Structure Padding concept while allocation of memory
using ''new'' ?


Structure padding occurs in arrays. new doesn''t allocate arrays, so it
can deal in the unpadded size of the type.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)- Hide quoted text -

- Show quoted text -

The output of sizeof(struct Dataunit) will definitely be different as
it takes into account the
padding based on the architecture(Processor).
Does ''new'' do not take the structure padding into account while size
calculation for allocating memory ?

Thx in advans,
Karthik Balaguru


这篇关于“新”不考虑结构填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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