f****y 发帖数: 70 | 1 这种情况怎么debug?
通常为什么会delete出错呢.
比如
unsigned char *buf = new unsigned char[4*4096];
程序最后在deconstruct里有
delete [] buf; 不过到这句就出错了. |
p******f 发帖数: 162 | 2
no problem in these lines, possible reasons are:
1) memory corrupted, such as a buffer overflow or underflow;
2) delete are called more than once.
【在 f****y 的大作中提到】 : 这种情况怎么debug? : 通常为什么会delete出错呢. : 比如 : unsigned char *buf = new unsigned char[4*4096]; : 程序最后在deconstruct里有 : delete [] buf; 不过到这句就出错了.
|
t****u 发帖数: 8614 | 3
3) buf pointer is changed.
【在 p******f 的大作中提到】 : : no problem in these lines, possible reasons are: : 1) memory corrupted, such as a buffer overflow or underflow; : 2) delete are called more than once.
|
t****u 发帖数: 8614 | 4 no offense, this code will probably cause problem,
cuz it is missing copy-ctor and assignment operator.
so in case of
int main()
{
A a;
A aa=a;
return 0;
}
it will crash. |
x***n 发帖数: 39 | 5 I guess this is what perlgulf said as:
"2) delete are called more than once. "
【在 t****u 的大作中提到】 : no offense, this code will probably cause problem, : cuz it is missing copy-ctor and assignment operator. : so in case of : int main() : { : A a; : A aa=a; : return 0; : } : it will crash.
|
b******u 发帖数: 10 | 6
Then declare copy-ctor and operator== private to forbid it being used this way.
【在 t****u 的大作中提到】 : no offense, this code will probably cause problem, : cuz it is missing copy-ctor and assignment operator. : so in case of : int main() : { : A a; : A aa=a; : return 0; : } : it will crash.
|
m***a 发帖数: 4 | 7 hey, thanks for pointing out that.
but anyone can explain this:
#include
class A
{
public:
static int i;
unsigned char * buf;
int j;
A(){i++; j = i; buf = new unsigned char[4*4096];cout<<"Construction:"<
endl;};
~A(){ delete [] buf;cout<<"Deconstruction:"<
};
int A::i;
main()
{
A a;
A aaa;
aaa = a;
A aaaa = a;
}
the output is
Construction:1
Construction:2
Deconstruction:1
Deconstruction:1
Deconstruction:1
Quite surpring! Why not prompt 'segmentation
【在 t****u 的大作中提到】 : no offense, this code will probably cause problem, : cuz it is missing copy-ctor and assignment operator. : so in case of : int main() : { : A a; : A aa=a; : return 0; : } : it will crash.
|
m***a 发帖数: 4 | 8 but why in the previous example, it prompt a 'segmentation
fault'? So weird! |
|
T***B 发帖数: 137 | |
T***B 发帖数: 137 | 10 case 1 calls constructor only once. But it tried to call destructor twice.
I guess that's why it crashes.
【在 T***B 的大作中提到】
|
t****t 发帖数: 6806 | 11 deleting a pointer more than once is undefined, so it can be anything:
segmentation fault, or whatever
【在 m***a 的大作中提到】 : hey, thanks for pointing out that. : but anyone can explain this: : #include : class A : { : public: : static int i; : unsigned char * buf; : int j; : A(){i++; j = i; buf = new unsigned char[4*4096];cout<<"Construction:"<
|
V******M 发帖数: 54 | 12 Exactly, that is why some people always put
ptr = null;
after
delete ptr;
when they release allocated memory.
BTW, deleting a null pointer is safe.
char[4*4096];cout<<"Construction:"<
【在 t****t 的大作中提到】 : deleting a pointer more than once is undefined, so it can be anything: : segmentation fault, or whatever
|