delete导致字符数组崩溃 [英] delete causes crash with character arrays

查看:96
本文介绍了delete导致字符数组崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GNU g ++ 3.3.3,Cygwin

//东西


静态字符* mbuffer = NULL;


//东西


void doit()

{

//东西

mbuffer = new(nothrow)char [1001];

断言(mbuffer!= NULL);


//东西


断言(mbuffer!= NULL);

delete [] mbuffer; //有时在这里崩溃(并不总是)。

mbuffer = NULL;


//东西


}


如何检测导致坠机的原因?


-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http: //mathforum.org/library/view/10978.html
http ://sourceforge.net/users/alexvn

GNU g++ 3.3.3, Cygwin
// Stuff

static char* mbuffer = NULL;

// Stuff

void doit()
{
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);

// Stuff

assert (mbuffer != NULL);
delete[] mbuffer; // Sometimes crashed here (not always).
mbuffer = NULL;

// Stuff

}

How can one detect what causes the crash?

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

推荐答案

Alex Vinokur写道:
Alex Vinokur wrote:
//东西
mbuffer = new(nothrow)char [1001];
断言(mbuffer!= NULL);


什么是nothrow?

delete [] mbuffer; //有时在这里崩溃(并非总是如此)。
mbuffer = NULL;
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);
What is nothrow?
delete[] mbuffer; // Sometimes crashed here (not always).
mbuffer = NULL;




看起来你正在删除由place new分配的内存

" normal"删除。这是非法的IIRC。您必须定义自己的

" placement delete"功能。


-

问候,

马蒂亚斯



It looks like you''re deleting memory allocated by placement new with a
"normal" delete. This is illegal IIRC. You will have to define your own
"placement delete" function.

--
Regards,
Matthias




" Matthias" <无**** @ digitalraid.com>在消息新闻中写道:cu ************* @ news.t-online.com ...

"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur写道:
Alex Vinokur wrote:
// Stuff
------------------------------------------- ------ mbuffer = new(nothrow)char [1001]


//程序在''delete [] mbuffer''下面的行崩溃(而不是行以上)

mbuffer =新字符[1001];

----------------------- -------------------------- assert(mbuffer!= NULL);
// Stuff ------------------------------------------------- mbuffer = new (nothrow) char [1001]
// The program crashes at ''delete[] mbuffer'' with the line below (instead of line above) too
mbuffer = new char [1001];
------------------------------------------------- assert (mbuffer != NULL);



什么是nothrow?



What is nothrow?

delete [] mbuffer; //有时在这里崩溃(并非总是如此)。
mbuffer = NULL;
delete[] mbuffer; // Sometimes crashed here (not always).
mbuffer = NULL;



看起来你正在删除由新的
分隔的内存分配的内存;正常"删除。这是非法的IIRC。您必须定义自己的
展示位置删除功能。

-
问候,
Matthias



It looks like you''re deleting memory allocated by placement new with a
"normal" delete. This is illegal IIRC. You will have to define your own
"placement delete" function.

--
Regards,
Matthias



-

Alex Vinokur
电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




" Matthias" <无**** @ digitalraid.com>在消息新闻中写道:cu ************* @ news.t-online.com ...

"Matthias" <no****@digitalraid.com> wrote in message news:cu*************@news.t-online.com...
Alex Vinokur写道:
Alex Vinokur wrote:
//东西
mbuffer = new(nothrow)char [1001];
断言(mbuffer!= NULL);
// Stuff
mbuffer = new (nothrow) char [1001];
assert (mbuffer != NULL);



什么是nothrow?



What is nothrow?




< QUOTE来自 http://groups-beta.google.com/group/...565d1c77795e52 >


如果你想要''新''要返回NULL而不是抛出异常,请使用


int * p = new(nothrow)int [array_size];


< /报价>


[snip]

-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/ view / 10978.html
http://sourceforge.net/users/alexvn



<QUOTE from http://groups-beta.google.com/group/...565d1c77795e52 >

If you want ''new'' to return NULL instead of throwing an exception, use

int* p = new (nothrow) int[array_size];

</QUOTE>

[snip]
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


这篇关于delete导致字符数组崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆