从c'tor扔出去 [英] throw from c'tor

查看:114
本文介绍了从c'tor扔出去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能造成内存泄漏,下面的代码如果我运行

行:

------------- --------------------------------------------

MyClass cl = new MyClass();

-------------------------------- -------------------------

???


- -------------------------------------------------- -----

MyClass :: MyClass()

{

try {

int * a = 0;

int * b = 0;

a = new int [X];

b = new int [X];

} catch(...){

如果(a)删除[] a;

如果(b)删除[] b;

throw;

}

}

------------------ ---------------------------------------


我知道,从c''tor抛出东西并不是那么聪明。


但上面的代码有什么问题吗?

谢谢!

PS:我在C ++工作了3年,但是在我用Java工作了3年之后,

也是如此。所以返回有点嗯...

Is there a possibility to create memory leak, the code below if I run
the line:
---------------------------------------------------------
MyClass cl = new MyClass();
---------------------------------------------------------
???

---------------------------------------------------------
MyClass::MyClass()
{
try {
int *a = 0;
int *b = 0;
a = new int[X];
b = new int[X];
} catch (...) {
if (a) delete[] a;
if (b) delete[] b;
throw;
}
}
---------------------------------------------------------

I know, it is not so clever to throw things from a c''tor.

But code above has any problem?
thanks!
PS: I worked in C++ for 3 years but after I worked in Java for a 3 years
too. So the returning is a bit hmmm...

推荐答案

Chameleon写道:
Chameleon wrote:

是否有可能造成内存泄漏,下面的代码如果我运行

行:

---------------- -----------------------------------------

MyClass cl = new MyClass();
Is there a possibility to create memory leak, the code below if I run
the line:
---------------------------------------------------------
MyClass cl = new MyClass();



你的意思是:


MyClass * cl = new MyClass();



did you mean:

MyClass * cl = new MyClass();

?


------------------------------ ---------------------------

???


-------------------------------------------------- -------

MyClass :: MyClass()

{

尝试{

int * a = 0;

int * b = 0;

a = new int [X];

b = new int [X];

} catch(...){

如果(a)删除[] a;

如果(b)删除[] b;
---------------------------------------------------------
???

---------------------------------------------------------
MyClass::MyClass()
{
try {
int *a = 0;
int *b = 0;
a = new int[X];
b = new int[X];
} catch (...) {
if (a) delete[] a;
if (b) delete[] b;



空指针上的删除是OK - 无需进行空检查。

delete on a null pointer is OK - no need to do null check.


throw;

}

}

------ -------------------------------------------------- -


我知道,从c''tor扔东西并不是那么聪明。


但上面的代码有什么问题吗?
throw;
}
}
---------------------------------------------------------

I know, it is not so clever to throw things from a c''tor.

But code above has any problem?



a更好的想法是使用向量...


MyClass :: MyClass()

{

vector< int> a(X);

vector< int> b(X);

}


....更容易阅读,更容易出错

a better idea is to use a vector...

MyClass::MyClass()
{
vector<int> a(X);
vector<int> b(X);
}

.... much easier to read and much harder to get wrong


>


谢谢!


PS:我在C ++工作了3年但是在我从事Java工作之后3年

。所以返回有点嗯......
>

thanks!
PS: I worked in C++ for 3 years but after I worked in Java for a 3 years
too. So the returning is a bit hmmm...




Chameleon skrev:

Chameleon skrev:

是否有可能造成内存泄漏,下面的代码如果我运行

行:

---------- -----------------------------------------------

MyClass cl = new MyClass();

----------------------------- ----------------------------

???
Is there a possibility to create memory leak, the code below if I run
the line:
---------------------------------------------------------
MyClass cl = new MyClass();
---------------------------------------------------------
???



No.

No.


>

------ -------------------------------------------------- -

MyClass :: MyClass()

{

尝试{

int * a = 0;

int * b = 0;

a = new int [X];

b = new int [X];

} catch(...){

如果(a)删除[] a;

如果(b)删除[] b;

抛出;

}

}
>
---------------------------------------------------------
MyClass::MyClass()
{
try {
int *a = 0;
int *b = 0;
a = new int[X];
b = new int[X];
} catch (...) {
if (a) delete[] a;
if (b) delete[] b;
throw;
}
}



正如Gianni所说,使用std更好:)向量。它更像是b b b和bb,它和原始指针一样快。然而,最重要的是,它将自动化。你所有的工作。复制构造和赋值

都可以通过使用编译器内置功能来实现。

As Gianni said, it is much better to use a std::vector. It is more
flexibe and just as fast as raw pointers. Most important, however is
that it will "automate" all your work. Copy constructing and assignment
can all be made to work by using the compilers built-in functionality.


---------------------------------- -----------------------


我知道,从c'扔东西并不是那么聪明托尔。
---------------------------------------------------------

I know, it is not so clever to throw things from a c''tor.



这绝对没有错。相反:它是

表示施工失败的唯一合理方式。

[snip]


/ Peter

There is absolutely nothong wrong about that. On the contrary: it is
the only reasonable way to indicate failure of construction.
[snip]

/Peter




Chameleon写道:

Chameleon wrote:

是否有可能创建内存泄漏,下面的代码如果我运行

行:

--------------------- ------------------------------------

MyClass cl = new MyClass( );
Is there a possibility to create memory leak, the code below if I run
the line:
---------------------------------------------------------
MyClass cl = new MyClass();



MyClass * p_cl = new MyClass;


甚至更好:


#include< boost / shared_ptr.hpp>

boost :: shared_ptr< MyClass bsp(new MyClass);

MyClass* p_cl = new MyClass;

or even better:

#include <boost/shared_ptr.hpp>
boost::shared_ptr< MyClass bsp( new MyClass );


--------------------------- ------------------------------
---------------------------------------------------------



为什么不:


模板< typename T>

class MyClass

{

std :: vector< T va;

std :: vector< T vb;

public:

MyClass():va(),vb(){}

MyClass(s​​ize_t);

};

Why not:

template< typename T >
class MyClass
{
std::vector< T va;
std::vector< T vb;
public:
MyClass() : va(), vb() { }
MyClass(size_t);
};


--------------------------- ------------------------------

MyClass :: MyClass()
---------------------------------------------------------
MyClass::MyClass()



template< typename T>

MyClass< T> :: Myclass(s​​ize_t size):va(大小),vb(大小)

{

}

template< typename T >
MyClass< T >::Myclass(size_t size) : va(size), vb(size)
{
}


{

尝试{

int * a = 0;

int * b = 0;

a = new int [X];

b = new int [X];

} catch(...){

if(a) )删除[] a;

如果(b)删除[] b;

throw;

}

}

----------------------------------------- ----------------


我知道,从c''tor扔东西并不是那么聪明。
{
try {
int *a = 0;
int *b = 0;
a = new int[X];
b = new int[X];
} catch (...) {
if (a) delete[] a;
if (b) delete[] b;
throw;
}
}
---------------------------------------------------------

I know, it is not so clever to throw things from a c''tor.



从ctor投掷没有错,但不是这样的。使用std :: bad_alloc明确地捕获


Nothing wrong with throwing from a ctor, but not like this. catch
explicitly using std::bad_alloc.


>

但上面的代码有什么问题吗?
>
But code above has any problem?



是的,如果第二次分配产生了一个std :: bad_alloc,那么你需要
有泄漏

这就是为什么智能指针会更好。

使用向量时,不需要这些。

Yes it does, if the second allocation generates a std::bad_alloc, you
have a leak
Thats why a smart pointer would be preferable.
With vectors, none of this is needed.


>


谢谢!


PS:我在C ++工作了3年但是在我用Java工作了3年之后

。所以返回有点嗯......
>

thanks!
PS: I worked in C++ for 3 years but after I worked in Java for a 3 years
too. So the returning is a bit hmmm...


这篇关于从c'tor扔出去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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