为什么new []为返回的地址添加4个字节 [英] Why new[] adds 4 bytes to the returned address

查看:78
本文介绍了为什么new []为返回的地址添加4个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MyClass * obj = new MyClass [3];


即使new []返回的地址是0x00365f50,

但是obj会自动更新0x00365f54。

为什么以及谁将这些addtional 4字节添加到地址?

解决方案

JeanDean写道:


MyClass * obj = new MyClass [3];


即使new []返回的地址是0x00365f50,



你怎么知道的?


但obj会自动更新为0x00365f54。

为什么以及谁将这些addtional 4字节添加到地址?



猜测:4个字节包含某种控制块

实现成功解除分配

内存(可能是你请求的数组的大小)...


你试过看看这4个字节包含什么吗?


V

-

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

我没有回应top-张贴回复,请不要问


2月28日下午2:57,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:


JeanDean写道:


MyClass * obj = new MyClass [3];


即使new []返回的地址是0x00365f50,



你怎么样?知道吗?


但obj会自动更新0x00365f54。

为什么以及谁将这些addtional 4字节添加到地址?



推测:4个字节包含某种控制块

实现成功解除分配

内存(可能是你请求的数组的大小)...


你试过看看这4个字节包含什么吗?


V

-

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

我没有回应top-发布回复,请不要问



实际上,它存储了数组元素的数量:


例如。代码输出0000000B

///////////////////////

MyClass * obj =新的MyClass [11];

char * ptr =(char *)obj;

ptr =(char *)obj - sizeof(int);

printf("%p",* ptr);

/////////////////////


但问题是,在VC ++中它保留了4个字节,但是如果我用g ++编译相同的

代码,它会保留8个字节。

I我无法理解这个额外的

内存预留的标准是什么?


JeanDean写道:
< blockquote class =post_quotes>
>

但问题是,在VC ++中它保留了4个字节,但是如果我用g ++编译相同的

代码它保留相同的8个字节。

我无法理解这个额外的

内存预留的标准是什么?



问题在哪里?这是一个植入问题,你永远不会担心
担心。


-

Ian Collins。


MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,
but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?

解决方案

JeanDean wrote:

MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,

How do you know that?

but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?

Speculation: the 4 bytes contain some kind of control block
needed for the implementation to successfully deallocate the
memory (could be the size of the array you requested)...

Have you tried to see what those 4 bytes contain?

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


On Feb 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

JeanDean wrote:

MyClass *obj = new MyClass[3];

Even though address returned by new [] is 0x00365f50,


How do you know that?

but obj gets automatically updated by 0x00365f54.
Why and who adds these addtional 4 bytes to the address?


Speculation: the 4 bytes contain some kind of control block
needed for the implementation to successfully deallocate the
memory (could be the size of the array you requested)...

Have you tried to see what those 4 bytes contain?

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

Actually, it is storing number of elements of the array:

eg. code outputs 0000000B

///////////////////////
MyClass* obj = new MyClass[11];
char* ptr = (char*)obj;
ptr = (char*)obj - sizeof(int);
printf("%p",*ptr);
/////////////////////

But the problem is , in VC++ it reserves 4 Bytes but if I compile same
code in g++ it reserves 8 bytes for the same.
I am not able to understand what is the standard for this additional
memory reservation?


JeanDean wrote:

>
But the problem is , in VC++ it reserves 4 Bytes but if I compile same
code in g++ it reserves 8 bytes for the same.
I am not able to understand what is the standard for this additional
memory reservation?

Where''s the problem? It''s an implantation issue, which you never have
to worry about.

--
Ian Collins.


这篇关于为什么new []为返回的地址添加4个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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