class Foo { // ... } int main() { char * p_char = new char [5000]; // p_char充满数据从插座中读取 Foo * p_foo =(Foo *)p_char; // p_foo在这里使用... 删除p_foo; } 假设Foo小于5000 * sizeof(char),这会导致 内存泄漏?或者删除调用是否知道分配的内存比sizeof(Foo)更多,并且做了正确的事情? 解决方案 BigBrian写道: 假设Foo小于5000 * sizeof(char),会导致内存泄漏吗?或者删除调用是否知道分配的内存比sizeof(Foo)更多,并做正确的事情? 无论如何都不保证。这样做: p_foo-> ~Foo(); //销毁对象 delete [] p_char; //释放记忆 - Pete Becker Dinkumware,Ltd。( http://www.dinkumware.com ) Pete Becker< pe ******** @ acm.org>在新闻中写道:pOudnZ0dm6lIn87fRVn- [email protected] : BigBrian写道: 假设Foo小于5000 * sizeof(char),会导致内存泄漏吗?或者删除调用是否知道分配的内存比sizeof(Foo)更多,并做正确的事情? 无论如何都不保证。这样做: p_foo-> ~Foo(); //销毁对象 delete [] p_char; //释放记忆 即使这样也不安全。请注意,没有提到* p_foo已经构建了!并且,不是OP的东西未定义的行为 无论如何......他正在对内存进行删除,这是新的[]'ed(忽略了 他也在切换类型......)。 Andre Kostur写道: 即便如此不安全。请注意,没有提到* p_foo已经构建过了! 还没有被提及与没有发生并不相同。我很愿意明确假设读入的数据产生了一个正确初始化的对象。但是如果你想问他这件事,请继续。 并且,不是OP的东西未定义的行为无论如何...他正在删除在内存上是新的[]''ed(忽略了他也在切换类型......)。 这可能是什么& ;无论如何都不保证是什么意思? - Pete Becker Dinkumware,Ltd。( http://www.dinkumware.com ) I realize this isn''t the best code, but I''m doing maintenance on alegcy system that has something similar to the following...class Foo{//...}int main(){char * p_char = new char[5000];// p_char gets filled with data read from a socketFoo * p_foo = (Foo *) p_char;// p_foo gets used here...delete p_foo;}Assuming Foo is smaller than 5000 * sizeof(char), will this result in amemory leak? Or will the call to delete know that more memory wasallocated than sizeof(Foo), and do the right thing? 解决方案 BigBrian wrote: Assuming Foo is smaller than 5000 * sizeof(char), will this result in a memory leak? Or will the call to delete know that more memory was allocated than sizeof(Foo), and do the right thing?No guarantees either way. Do it like this:p_foo->~Foo();// destroy objectdelete [] p_char;// free the memory--Pete BeckerDinkumware, Ltd. (http://www.dinkumware.com)Pete Becker <pe********@acm.org> wrote in news:pOudnZ0dm6lIn87fRVn- [email protected]: BigBrian wrote: Assuming Foo is smaller than 5000 * sizeof(char), will this result in a memory leak? Or will the call to delete know that more memory was allocated than sizeof(Foo), and do the right thing? No guarantees either way. Do it like this: p_foo->~Foo(); // destroy object delete [] p_char; // free the memoryEven that''s not safe. Note that it hasn''t been mentioned that *p_foo hasever been constructed! And, isn''t the OP''s stuff undefined behaviouranyway... he''s doing a delete on memory that was new[]''ed (ignoring thathe''s switching types too...).Andre Kostur wrote: Even that''s not safe. Note that it hasn''t been mentioned that *p_foo has ever been constructed!"Hasn''t been mentioned" is not the same as "hasn''t happened." I''mwilling to make the obvious assumption that the data read in produces aproperly initialized object. But if you want to ask him about it, go ahead. And, isn''t the OP''s stuff undefined behaviour anyway... he''s doing a delete on memory that was new[]''ed (ignoring that he''s switching types too...).Could that be what "No guarantees either way" means?--Pete BeckerDinkumware, Ltd. (http://www.dinkumware.com) 这篇关于内存分配/解除分配问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 18:39