确保释放的动态内存返回堆? [英] making sure that the deallocated dynamically memory returns to the heap?

查看:71
本文介绍了确保释放的动态内存返回堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在面试中被问了一个问题..


它与动态分配和释放的内存有关。


例如。

//开始

char * p =新字符[1000];

...

....

删除[] p;

//结束


问题是..我怎么能确定已经解除分配的内存返回

到堆?

我回答说p = NULL.But再次面试官可能是/>
期待随后辩论的不同答案...


所以对我来说,我的动态分配和解除分配的知识

memroy开始使用new并以删除结束...

那么有没有确保将释放的(或

释放的)内存返回到堆中?


谢谢和问候,

Yogesh

[见 http://www.go tw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hello All,

I was asked a question in an interview..

Its related to dynamically allocated and deallocated memory.

eg.
//start
char * p = new char[1000];
...
....
delete [] p;
//end

The quesiton was..How will I be sure the deallocated memory is returned
to heap?
I answered make p = NULL.But again the interviewer was probably
expecting different answer as the debate went on thereafter...

So for me my knoweldge about dynamically allocated and deallocated
memroy starts with new and ends with delete...
So is there any such thing like making sure that the deallocated(or
freeed) memory is returned to the heap?

Thanks and Regards,
Yogesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

据我所知,在调用delete []后,p内存自动返回到

堆。因此,这是操作员删除(和[])的责任。


也许这是一个技巧问题?


最好,

Zaharije Pasalic

As far as I know, after calling delete[]p memory is automaticly returned to
heap. So, this is responsibility of operator delete (and []).

Maybe this was one of trick-questions?

Best,
Zaharije Pasalic


[交叉发布已删除]

yo ****** @ gmail.com 写道:
[Cross-posting deleted]

yo******@gmail.com wrote:
大家好,

我是在一次采访中问了一个问题..

它与动态分配和解除分配的内存有关。

例如。
//开始
char * p =新的字符[1000];
..
...
删除[] p;
//结束

问题是......如何我确定将已释放的内存返回堆中?
我回答make p = NULL。但是,随后辩论继续进行,面试官可能会期待不同的答案...... />
所以对我来说,我的知识关于动态分配和解除分配
memroy从new开始,以delete结束......
那么有没有像确保将释放的(或
释放的)内存返回到堆?
Hello All,

I was asked a question in an interview..

Its related to dynamically allocated and deallocated memory.

eg.
//start
char * p = new char[1000];
..
...
delete [] p;
//end

The quesiton was..How will I be sure the deallocated memory is returned
to heap?
I answered make p = NULL.But again the interviewer was probably
expecting different answer as the debate went on thereafter...

So for me my knoweldge about dynamically allocated and deallocated
memroy starts with new and ends with delete...
So is there any such thing like making sure that the deallocated(or
freeed) memory is returned to the heap?




如果代码进入delete []语句和地址由

p保持未被更改,它肯定会被释放(或返回到

堆),除非全局new []和delete []运算符是超载

做一些与普通运营商不同的事情。之后使指针

NULL对堆没有影响,但如果

指针仍在后续代码中的范围内,那么这是很好的做法。


所以,你要确保new []和delete []没有重载,

make pa" char * const"而不是char *,并确保没有例外

被抛出在...中。区域。更好的方法是使用一个聪明的

指针来处理所有已分配的内存,这样你就不会忘记释放

它,所以代码是异常安全的。

干杯! --M



If the code makes it to the delete[] statement and the address held by
p has not been changed, it will certainly be freed (or "returned to the
heap") unless the global new[] and delete[] operators were overloaded
to do something different than the usual operators. Making the pointer
NULL afterwards has no effect on the heap, but it is good practice if
the pointer is still in scope in subsequent code.

So, you''ll want to make sure new[] and delete[] are not overloaded,
make p a "char*const" instead of "char*", and make sure no exceptions
are thrown in the "..." region. Better still would be to use a smart
pointer to handle all allocated memory so you don''t forget to release
it and so the code is exception-safe.
Cheers! --M


mlimber写道:
mlimber wrote:
[交叉发布已删除]

> yo******@gmail.com 写道:
[Cross-posting deleted]

yo******@gmail.com wrote:
Hello All,

我在接受采访时被问到了一个问题。

它与动态分配和释放的内存有关。

例如。
//开始
char * p =新字符[1000];
..
...
删除[] p;
//结束
我回答说p = NULL。但是面试官可能会再次期待不同的答案此后辩论继续进行......

所以对我来说,关于动态分配和解除分配的知识是从新的开始到以删除结束......
所以有没有这样的事情比如确保将释放的(或
释放的)内存返回到堆中?
如果代码生成它删除[]语句并且
p持有的地址没有被更改,它肯定会被释放(或返回到
堆),除非全局new []和delete []运营商超负荷做了与通常的运营商不同的事情。之后使指针
NULL对堆没有影响,但如果指针仍在后续代码中的范围内,那么这是一个好习惯。
Hello All,

I was asked a question in an interview..

Its related to dynamically allocated and deallocated memory.

eg.
//start
char * p = new char[1000];
..
...
delete [] p;
//end

The quesiton was..How will I be sure the deallocated memory is returned
to heap?
I answered make p = NULL.But again the interviewer was probably
expecting different answer as the debate went on thereafter...

So for me my knoweldge about dynamically allocated and deallocated
memroy starts with new and ends with delete...
So is there any such thing like making sure that the deallocated(or
freeed) memory is returned to the heap?
If the code makes it to the delete[] statement and the address held by
p has not been changed, it will certainly be freed (or "returned to the
heap") unless the global new[] and delete[] operators were overloaded
to do something different than the usual operators. Making the pointer
NULL afterwards has no effect on the heap, but it is good practice if
the pointer is still in scope in subsequent code.




我会说它通常不是很好的做法,因为它可能隐藏双重

删除错误。

所以,你会想确定new []和delete []没有重载,
make pa" char * const"而不是char *,并确保在...中抛出没有例外
区域。


另外,当然之间不应该有返回语句,如果新的[]和delete []在循环中,那么

,no继续或休息。

更好的是使用智能指针来处理所有已分配的内存,这样你就不会忘记释放它,因此代码是异常的
异常-safe。



I''d say it''s not generally good practice, because it might hide double
deletion errors.
So, you''ll want to make sure new[] and delete[] are not overloaded,
make p a "char*const" instead of "char*", and make sure no exceptions
are thrown in the "..." region.
In addition, there should of course be no return statement in between, and
if the new[] and delete[] are inside a loop, no continue or break.
Better still would be to use a smart pointer to handle all allocated
memory so you don''t forget to release it and so the code is
exception-safe.




对于上面的例子,类似于std :: auto_ptr,但对于数组

将是一个很好的候选者。 br />



For the above example, something similar to std::auto_ptr, but for arrays
would be a good candidate.


这篇关于确保释放的动态内存返回堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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