本文介绍了代码中的内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 如果在指针b指向的内存中分配内存时,是否存在bad_alloc,是否应删除指针a指向的内存?我不确定 如果我不删除,是否会有内存泄漏。Hello everyone,Should I delete memory pointed by pointer a if there is bad_alloc whenallocating memory in memory pointed by pointer b? I am not surewhether there will be memory leak if I do not delete a. 展开 | 选择 | 换行 | 行号推荐答案在2008-01-05 16:04,George2写道:On 2008-01-05 16:04, George2 wrote: 大家好, 当指针b指向的内存中分配内存时,如果有bad_alloc,我应该删除指针a指向的内存吗?我不确定 如果我不删除,是否会有内存泄漏。Hello everyone,Should I delete memory pointed by pointer a if there is bad_alloc whenallocating memory in memory pointed by pointer b? I am not surewhether there will be memory leak if I do not delete a. 展开 | 选择 | 换行 | 行号" Erik Wikstr?m" < Er *********** @ telia.comwrote in message news:zP ***************** @ newsb .telia.net ..."Erik Wikstr?m" <Er***********@telia.comwrote in messagenews:zP*****************@newsb.telia.net... 是的,但是如果你在分配时遇到bad_alloc怎么办?然后你 将尝试删除指向垃圾的任何内存。要 防止这样使用这样的东西: 尝试{ int * a = 0; a = new int [N]; int * b = new int [N]; } catch(bad_alloc) { 删除[] a; }Yes, but what about if you get a bad_alloc when allocating a? Then youwill try to delete whatever memory that the garbage in a points to. Toprevent this use something like this instead:try { int* a = 0; a = new int[N]; int* b = new int[N];}catch (bad_alloc){ delete[] a;} 好​​吧,这个想法是正确的,但实现是错误的,因为删除语句中的范围将超出范围。此外,除非a和b在try中删除了,如果成功,代码将泄漏内存,因为一旦a和 b超出范围,就会有没有机会删除内存。 替代方案: int * a = 0; 试试{ a = new int [N]; int * b = new int [N]; //做额外的在这里工作 删除[] b; 删除[] a; } catch(bad_alloc){ 删除[] a; } 我同意你的看法,智能指针就是这里的方式:-)Well, the idea is right but the implementation is wrong, because a will beout of scope in the delete statement. Moreover, unless a and b are deletedinside the try, the code will leak memory if it succeeds, because once a andb are out of scope, there''s no further opportunity to delete the memory.An alternative:int* a = 0;try {a = new int[N];int* b = new int[N];// do additional work heredelete[] b;delete[] a;} catch (bad_alloc) {delete[] a;}I agree with you that smart pointers are the way to go here :-) 1月5日上午10:04,George2< george4acade ... @ yahoo.comwrote:On Jan 5, 10:04 am, George2 <[email protected]: 大家好, 如果在指针b指向的内存中分配内存时,是否存在bad_alloc,是否应删除指针a指向的内存?我不确定 如果我不删除,是否会有内存泄漏。 [code] try { a = new int [N]; b = new int [M];} catch(bad_alloc) { //如果成功,但是b失败,我们是否应该尝试删除[] a 避免内存泄漏?Hello everyone,Should I delete memory pointed by pointer a if there is bad_alloc whenallocating memory in memory pointed by pointer b? I am not surewhether there will be memory leak if I do not delete a.[code]try { a = new int [N]; b = new int [M];} catch (bad_alloc){ // if a success, but b fail, should we try to delete[] a here toavoid memory leak? 我有疑问,所以让我们来看看,福尔摩斯先生: #include< iostream> #include< stdexcept> A级 { 静态计数; 公开: A() { std :: cout<< A()\ n;; if(2 == count) throw std :: bad_alloc(); count ++ ; } ~A(){std :: cout<< "〜A()\\\英寸; } }; int A :: count; B级 { public: B(){std :: cout<< " B()\\\英寸; } ~B(){std :: cout<< "〜B()\\\英寸; } }; int main() { A * p_a = 0; B * p_b = 0; 尝试{ p_b = new B [5]; p_a =新的A [5]; } catch(const std :: bad_alloc& e) { std :: cout<< " error:"; std :: cout<< e.what()<< std :: endl; } } / * B() B() B() B() B()//显然,出了点问题 A() A() A() ~A() ~A() 错误:St9bad_alloc * / 所以我们肯定有问题。 是删除[] p_a的解决方案吗? 没有。这是错误的解决方案,有一种更简单,更好的方法。 std :: vector拥有它的元素,除非这些是指针。 也是,std :: vector调用复制构造以填充元素 创建时间。 因此,为了跟踪发生的情况,我们需要将A'的复制文件修改为 增加一个静态计数器。 .... #include< vector> A级 { static int copy_count; static int count; public: ... //'副本ctor A :: A(const A& copy) { std :: cout<< copy A \ n;; if(2 == copy_count) throw std :: bad_alloc(); copy_count ++; } ... }; int A :: count; int A :: copy_count; int main() { 尝试 { std :: vector< B vb(5); std :: vector<一个va(5); } catch(const std :: bad_alloc& e) { std :: cout<< " error:"; std :: cout<< e.what()<< std :: endl; } } / * B() ~B()//整个向量vb用副本初始化 A()//默认ctor 复制A // copy_count = 0 复制A 复制A // copy_count = 2,kaboom! ~A() ~ A() ~A() ~B()//自动 ~B() ~B() ~B() ~B() 错误:St9bad_alloc * / 道德:当你不确定时,测试它。 并证明解决方案。 不要使用指针,你可以使用boost :: scoped_array。I have doubts, so lets find out, Mr Holmes:#include <iostream>#include <stdexcept>class A{static int count;public:A(){std::cout << "A()\n";if(2 == count)throw std::bad_alloc();count++;}~A() { std::cout << "~A()\n"; }};int A::count;class B{public:B() { std::cout << "B()\n"; }~B() { std::cout << "~B()\n"; }};int main(){A* p_a = 0;B* p_b = 0;try {p_b = new B[5];p_a = new A[5];} catch (const std::bad_alloc& e){std::cout << "error: ";std::cout << e.what() << std::endl;}}/*B()B()B()B()B() // obviously, something is wrongA()A()A()~A()~A()error: St9bad_alloc*/So we definitely have a problem.Is the solution to delete [] p_a?no. thats the wrong solution, there is a simpler, better way.A std::vector own its elements unless these are pointers.also, a std::vector invokes copy construction to populate elements atcreation time.So to track what is happening, we need to modify A''s copy ctor toincrement a static counter.....#include <vector>class A{static int copy_count;static int count;public:...// A''s copy ctorA::A( const A& copy){std::cout << "copy A\n";if(2 == copy_count)throw std::bad_alloc();copy_count++;}...};int A::count;int A::copy_count;int main(){try{std::vector< B vb(5);std::vector< A va(5);}catch (const std::bad_alloc& e){std::cout << "error: ";std::cout << e.what() << std::endl;}}/*B()~B() // the entire vector vb is initialized with a copyA() // default ctorcopy A // copy_count = 0copy Acopy A // copy_count = 2, kaboom!~A()~A()~A()~B() // automatic~B()~B()~B()~B()error: St9bad_alloc*/moral: When you aren''t sure, test it.And prove the solution.Don''t use pointers, you could use a boost::scoped_array. 这篇关于代码中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 20:40