由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - What is wrong?
相关主题
one question about overloading operator deletelinux 能查到 deleted file list 吗
题2a c++ question
pointer to functionnew了指针,delete的时候出错了
C++弱问问个C++中重复删除指针的问题
问个无厘头问题[合集] 请教一个c++ 中 delete [] 的问题
new一定要和delete配对吗?问个 ctor/copy ctor的问题
请教 C++的一个困惑 (operator delete)delete sheets from Excel workbook in C# (转载)
C++ delete发个初级面试题
相关话题的讨论汇总
话题: ctest话题: test话题: function话题: delete话题: what
进入Programming版参与讨论
1 (共1页)
f******y
发帖数: 2971
1
In the following program, obviously there is a memory leak. Other than that,
I see memory newed in the main function is deleted twice. When I ran it, it
did not crash however. Anybody can explain it? Thanks
#include
class Ctest {
public:
Ctest(){}
~Ctest(){}
double m_value[200];
};
void function(Ctest* test) {
delete test;
test = new Ctest();
}
int main() {
Ctest* t = new Ctest();
function(t);
delete t;
return 0;
r****t
发帖数: 10904
2
你 new 了两次, delete 了两次吧

that,
it

【在 f******y 的大作中提到】
: In the following program, obviously there is a memory leak. Other than that,
: I see memory newed in the main function is deleted twice. When I ran it, it
: did not crash however. Anybody can explain it? Thanks
: #include
: class Ctest {
: public:
: Ctest(){}
: ~Ctest(){}
: double m_value[200];
: };

t****t
发帖数: 6806
3
it is deleted twice, since the memory allocated in function() did not get re
turned.
so it is undefined behaviour. quite often it will lead to program crash. but
quite as often it will not lead to crash. thus it is undefined.

【在 r****t 的大作中提到】
: 你 new 了两次, delete 了两次吧
:
: that,
: it

z****e
发帖数: 2024
4
void function(Ctest*& test)
这样才行。

【在 r****t 的大作中提到】
: 你 new 了两次, delete 了两次吧
:
: that,
: it

r****t
发帖数: 10904
5
明白了,谢谢。
d*****d
发帖数: 46
6
interesting, try this
void function(Ctest* test) {
delete test;
Ctest* test1 = new Ctest();
test = new Ctest();
delete test1;
}
you may see why...

that,
it

【在 f******y 的大作中提到】
: In the following program, obviously there is a memory leak. Other than that,
: I see memory newed in the main function is deleted twice. When I ran it, it
: did not crash however. Anybody can explain it? Thanks
: #include
: class Ctest {
: public:
: Ctest(){}
: ~Ctest(){}
: double m_value[200];
: };

1 (共1页)
进入Programming版参与讨论
相关主题
发个初级面试题问个无厘头问题
c++环境入门问题new一定要和delete配对吗?
如何在C里面call C++的routine呢请教 C++的一个困惑 (operator delete)
0 < -1 ? A c++ questionC++ delete
one question about overloading operator deletelinux 能查到 deleted file list 吗
题2a c++ question
pointer to functionnew了指针,delete的时候出错了
C++弱问问个C++中重复删除指针的问题
相关话题的讨论汇总
话题: ctest话题: test话题: function话题: delete话题: what