Redian新闻
>
new了指针,delete的时候出错了
avatar
new了指针,delete的时候出错了# Programming - 葵花宝典
f*y
1
这种情况怎么debug?
通常为什么会delete出错呢.
比如
unsigned char *buf = new unsigned char[4*4096];
程序最后在deconstruct里有
delete [] buf; 不过到这句就出错了.
avatar
p*f
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; 不过到这句就出错了.

avatar
t*u
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.

avatar
t*u
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.
avatar
x*n
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.

avatar
b*u
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.

avatar
m*a
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<endl;};
~A(){ delete [] buf;cout<};
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.

avatar
m*a
8
but why in the previous example, it prompt a 'segmentation
fault'? So weird!
avatar
T*B
9
avatar
T*B
10
case 1 calls constructor only once. But it tried to call destructor twice.
I guess that's why it crashes.

【在 T***B 的大作中提到】

avatar
t*t
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<
avatar
V*M
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<
【在 t****t 的大作中提到】
: deleting a pointer more than once is undefined, so it can be anything:
: segmentation fault, or whatever

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。