问题描述
大家好,
我在面试中被问了一个问题..
它与动态分配和释放的内存有关。
例如。
//开始
char * p =新字符[1000];
...
....
删除[] p;
//结束
问题是..我怎么能确定已经解除分配的内存返回
到堆?
我回答说p = NULL.But再次面试官可能是/>
期待随后辩论的不同答案...
所以对我来说,我的动态分配和解除分配的知识
memroy开始使用new并以删除结束...
那么有没有确保将释放的(或
释放的)内存返回到堆中?
谢谢和问候,
Yogesh
[见有关的信息]
[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保持未被更改,它肯定会被释放(或返回到
堆),除非全局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
我会说它通常不是很好的做法,因为它可能隐藏双重
删除错误。
所以,你会想确定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.
这篇关于确保释放的动态内存返回堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!