Sign-up....

请问返回一个动态对象的引用会不会造成内存泄漏?

如下所示

Test & myfun()

{

Test * test = new Test();

return *test;

}

Test &a=myfun();

......

这样用new分配的空间还能回收吗?

[113 byte] By [msdn] at [2007-9-26 8:20:36]
# 1 Re: 请问返回一个动态对象的引用会不会造成内存泄漏?

如下所示

Test & myfun()

{

Test * test = new Test();

return *test;

}

Test &a=myfun();

delete a;

......

这样用new分配的空间还能回收吗?

可以:

Test & myfun()

{

Test * test = new Test();

return *test;

}

Test &a=myfun();

delete a;

llf_hust at 2005-5-21 10:46:04 >
# 2 Re: 请问返回一个动态对象的引用会不会造成内存泄漏?

没对

Test & myfun()

{

Test * test = new Test();

return *test;

}

int main()

{

Test &a=myfun();

delete &a; //not delete a

}

cenlmmx at 2005-5-21 14:00:24 >
# 3 Re: 请问返回一个动态对象的引用会不会造成内存泄漏?

呵呵,,顶。。

mostideal at 2005-5-22 11:07:56 >

C/C++

All Classified