由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - new了指针,delete的时候出错了
相关主题
a c++ question[合集] 请教一个c++ 中 delete [] 的问题
one question about operator deleteone question about overloading operator delete
C++弱问What is wrong?
请问以下代码有什么错误?c++ 宏的问题
问个c++在不同函数里分配内存和释放内存的弱问题delete sheets from Excel workbook in C# (转载)
shared_ptr处理stack上面的指针c# question: char_array.ToString() is not working ?
linux 能查到 deleted file list 吗[转载] Re: 问个土问题吧
问个C++中重复删除指针的问题little endian vs big endian
相关话题的讨论汇总
话题: delete话题: buf话题: char
进入Programming版参与讨论
1 (共1页)
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
9
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

1 (共1页)
进入Programming版参与讨论
相关主题
little endian vs big endian问个c++在不同函数里分配内存和释放内存的弱问题
一个hash table的简单问题shared_ptr处理stack上面的指针
有人发过的一个面试题linux 能查到 deleted file list 吗
question about shift问个C++中重复删除指针的问题
a c++ question[合集] 请教一个c++ 中 delete [] 的问题
one question about operator deleteone question about overloading operator delete
C++弱问What is wrong?
请问以下代码有什么错误?c++ 宏的问题
相关话题的讨论汇总
话题: delete话题: buf话题: char