删除和例外 [英] delete and exception

查看:88
本文介绍了删除和例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

假设班级的人员投掷。因此,在这个时间点,已经分配了一些内存但是无法构造对象。此外,

内存尚未释放。 c ++ std会自动保证

调用操作符删除吗?


相关代码是:


class Int {

public:

Int()

{

抛100;

}

~Int()

{

//这个永远不会被调用,因为ctor会抛出。

}

};

int main()

{

Int * p = new Int;


//此时将被删除而不会显式调用

delete(如果ctor抛出)?

}

TC ++ PL Edition 3,14.4.5说应该释放内存。但它没有说清楚是否会通过致电运营商释放它是否会释放

delete()或其他东西。

提前致谢

Neelesh,

Hi all,
suppose the ctor of the class throws. Thus, at this point in time, some
memory has been allocated but the object couldnot be constructed. Also,
the memory is not yet freed. Does the c++ std automatically guarantee
the call to operator delete?

The relavent code is :

class Int {
public:
Int()
{
throw 100;
}
~Int()
{
// this will never be called, since the ctor throws.
}
};
int main()
{
Int *p = new Int;

// will p be deleted at this point without doing an explicit call to
delete (provided the ctor throws)?
}

TC++PL Edition 3, 14.4.5 says that the memory should be freed. But it
doesnot say clearly whether it will be freed by calling operator
delete() or something else.
Thanks in advance
Neelesh,

推荐答案

" Neelesh Bodas" <是ne *********** @ gmail.com>在消息中写道

新闻:11 ********************** @ o13g2000cwo.googlegr oups.com ...

:大家好,

:假设班级的人员投掷。因此,在这个时间点,已经分配了一些内存但是无法构造对象。另外,

:内存尚未释放。 c ++ std会自动保证

:对操作员删除的调用吗?

是的。


:TC ++ PL Edition 3 ,14.4.5说应该释放内存。但是它

:并不清楚它是否会被呼叫运营商释放

:delete()或其他东西。


每个新运算符都有一个相应的delete运算符,当分配对象的构造失败时,它自动被称为




在标准中,这是描述的在§5.3.4/ 17中:

<<<如果上面描述的对象初始化的任何部分终止于

抛出一个异常,并且可以找到一个合适的释放函数,

调用deallocation函数来释放其中的内存对象

正在构建,之后异常继续在

传播new-expression的上下文。如果没有明确匹配

释放函数,则传播异常不会导致对象的内存被释放。 [注意:当
被调用的分配函数不分配内存时,这是合适的;否则,它可能会导致内存泄漏。 ]>>>


这实际上是放置 - 删除操作符的(唯一)目的:

在您的上定义一个placement-new操作符时拥有,你还必须

提供一个匹配的放置 - 删除操作符(具有相同的额外

参数),这将被调用以释放已分配的内存
$ b如果构造函数抛出则为$ B.


hth -Ivan

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表单

Brainbench MVP for C ++<> http://www.brainbench.com
"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
: Hi all,
: suppose the ctor of the class throws. Thus, at this point in time, some
: memory has been allocated but the object couldnot be constructed. Also,
: the memory is not yet freed. Does the c++ std automatically guarantee
: the call to operator delete?
Yes.

: TC++PL Edition 3, 14.4.5 says that the memory should be freed. But it
: doesnot say clearly whether it will be freed by calling operator
: delete() or something else.

Each new operator has a corresponding delete-operator, which is called
automatically when the construction of an allocated object fails.

In the standard, this is described in §5.3.4/17:
<<< If any part of the object initialization described above terminates by
throwing an exception and a suitable deallocation function can be found,
the deallocation function is called to free the memory in which the object
was being constructed, after which the exception continues to propagate in
the context of the new-expression. If no unambiguous matching
deallocation function can be found, propagating the exception does not
cause the object''s memory to be freed. [Note: This is appropriate when
the called allocation function does not allocate memory; otherwise, it is
likely to result in a memory leak. ] >>>

This is actually the (only) purpose of placement-delete operators:
When defining a placement-new operator on your own, you must also
provide a matching placement-delete operator (with the same extra
parameters), which will be called to released allocated memory
in case a constructor throws.

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com



谢谢Ivan。


假设我将自己的运算符new和operator delete写入

之前的代码。 (注意 - 它们不是放置操作符)


#include< iostream>

使用命名空间std;


void * operator new(size_t t)//我自己的全局运算符new

{

cout<< operator new called << endl;

返回malloc(t);

}


void operator delete(void * mem)//对应的operator delete

{

cout<< operator delete called <<结束;

免费(mem);

}


//其余代码与上一篇文章中给出的相同,复制到这里

的完整性。


class Int {

public:

Int() {cout<< 构造函数调用 << ENDL;扔100; }

~Int(){cout<< destructor called << ENDL; }

};


int main()

{

Int * p = new Int ;

}

在这种情况下,我观​​察到消息operator new called并且

构造函数调用。此外,ctor抛出。但我没有看到消息

" operator delete called"打印到任何地方。

不应该使用

:: operator delete()释放:: operator new()分配的内存吗?

我正在使用g ++ 3.4.2。


我在这里缺少什么?


非常感谢,

Neelesh


Thanks Ivan.

So suppose I write my own operator new and operator delete to the
previous code. (Note - they are not placement operators)

#include <iostream>
using namespace std;

void * operator new (size_t t) // my own global operator new
{
cout << "operator new called" << endl;
return malloc(t);
}

void operator delete(void* mem) // corresponding operator delete
{
cout << "operator delete called" << endl;
free(mem);
}

// Rest of the code is same as given in the previous post, copied here
for completeness.

class Int {
public:
Int() { cout << "Constructor called " << endl; throw 100; }
~Int() { cout << "destructor called" << endl; }
};

int main()
{
Int *p = new Int;
}
In this case, I observe that the messages "operator new called" and
"Constructor called".Also, the ctor throws. But I donot see the message
"operator delete called" printed anywhere.
Should''nt the memory allocated by ::operator new() be freed using
::operator delete() ?

I am using g++ 3.4.2.

What am I exactly missing here?

Thanks a lot,
Neelesh




" Neelesh Bodas" <是ne *********** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...

"Neelesh Bodas" <ne***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...

谢谢Ivan。

因此,假设我将自己的operator new和operator delete写入
上一段代码。 (注意 - 它们不是放置操作符)

#include< iostream>
使用命名空间std;

void * operator new(size_t t)// my拥有全球运营商新的
{
cout<< operator new called << endl;
返回malloc(t);
}
void operator delete(void * mem)//对应的operator delete
{
cout< < operator delete called << endl;
免费(mem);
}
//其余的代码与上一篇文章中给出的相同,为了完整性而复制到这里
。 />
类Int {
公开:
Int(){cout<< 构造函数调用 << ENDL;扔100; }
~Int(){cout<< destructor called << ENDL; }
};

int main()
{
Int * p = new Int;
}


int main()

{

尝试{

Int * p = new Int;

}

catch(...){

std :: cerr<< \ n \ nnCaught \ n \ nn;;

}

}

在这种情况下,我观​​察到消息operator new called并且
构造函数被称为。也是,ctor抛出。但我没有看到消息
operator delete called打印到任何地方。
不应该使用
:: operator delete()释放:: operator new()分配的内存吗?

我正在使用g ++ 3.4.2 。

我在这里缺少什么?

Thanks Ivan.

So suppose I write my own operator new and operator delete to the
previous code. (Note - they are not placement operators)

#include <iostream>
using namespace std;

void * operator new (size_t t) // my own global operator new
{
cout << "operator new called" << endl;
return malloc(t);
}

void operator delete(void* mem) // corresponding operator delete
{
cout << "operator delete called" << endl;
free(mem);
}

// Rest of the code is same as given in the previous post, copied here
for completeness.

class Int {
public:
Int() { cout << "Constructor called " << endl; throw 100; }
~Int() { cout << "destructor called" << endl; }
};

int main()
{
Int *p = new Int;
}

int main()
{
try {
Int *p = new Int;
}
catch(...) {
std::cerr << "\n\nCaught\n\n";;
}
}

In this case, I observe that the messages "operator new called" and
"Constructor called".Also, the ctor throws. But I donot see the message
"operator delete called" printed anywhere.
Should''nt the memory allocated by ::operator new() be freed using
::operator delete() ?

I am using g++ 3.4.2.

What am I exactly missing here?




IIRC,如果你没有捕获异常,将调用terminate()。终止()的

默认行为是调用abort()。


问候,

Sumit。

-

Sumit Rajan< su **** @ msdc.hcltech.com>



IIRC, if you do not catch an exception, terminate() will be called. The
default behavior of terminate() is to call abort().

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>


这篇关于删除和例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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