析构函数的问题 [英] problem with destructor

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

问题描述

嗨!

我遇到这种情况有问题。我有一个像下面这样的类,并且有一些指针(ptr1和ptr2)。我在构造函数中动态分配内存

我在析构函数中释放。对于一个指针,当

dustroctor删除对象时会出现一些错误。为什么?我需要更改什么?


请帮助。


bart


A级< br $>
{

char * ptr1;

char * ptr2;

A(char * string1,char * string2)

{

if(string1!= NULL)

{

ptr1 = new char [strlen(string1) +1];

strcpy(ptr1,string1);

}

if(string2!= NULL)

{

ptr2 = new char [strlen(string2)+1];

strcpy(ptr2,string2);

}

}

虚拟~A()

{

if(ptr1!= NULL)

删除[] this-> ptr1;

if(ptr2!= NULL)

delete [] this-> ptr2;

}

//其他方法。我在那里使用ptr1和ptr2

}


main()

{


MyClass(" text1"," text2")

返回0;

}

Hi!
I have a problem with such situation. I have a class like below and have
some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor
and I free in destructor. For one pointer there is some error when
dustroctor delete objects. Why? What should I have to change?

Please help.

bart

class A
{
char *ptr1;
char *ptr2;
A(char *string1, char *string2)
{
if (string1 != NULL)
{
ptr1 = new char[strlen(string1)+1];
strcpy(ptr1, string1);
}
if (string2 != NULL)
{
ptr2 = new char[strlen(string2)+1];
strcpy(ptr2, string2);
}
}
virtual ~A()
{
if (ptr1 != NULL)
delete [] this->ptr1;
if (ptr2 != NULL)
delete [] this->ptr2;
}
// other methods. I use there ptr1 and ptr2
}

main()
{

A MyClass("text1", "text2")
return 0;
}

推荐答案



" bArT" < BA ***** @ poczta.onet.pl> schrieb im Newsbeitrag

news:ce ********** @ nemesis.news.tpi.pl ...

"bArT" <ba*****@poczta.onet.pl> schrieb im Newsbeitrag
news:ce**********@nemesis.news.tpi.pl...
嗨!
我这种情况有问题。我有一个类如下,
有一些指针(ptr1和ptr2)。我在
构造函数中动态分配内存,我在析构函数中释放。对于一个指针,当
dustroctor删除对象时会出现一些错误。为什么?我应该改变什么?

请帮忙。

bart

A类
{char / ptr1;
char * ptr2;


A(){ptr1 = NULL; ptr2 = NULL;}


A(char * string1,char * string2)
{


ptr1 = NULL; PTR2 = NULL; //因为string1或string2可能是

NULL ??

if(string1!= NULL)
{
ptr1 = new char [strlen( string1)+ 1];
strcpy(ptr1,string1);
}
if(string2!= NULL)
{
ptr2 = new char [strlen(string2 )+1];
strcpy(ptr2,string2);
}
}
虚拟~A()
{
if(ptr1!= NULL) )
删除[] this-> ptr1;
if(ptr2!= NULL)
delete [] this-> ptr2;


ptr1 = NULL; PTR2 = NULL; //在删除[]后总是这样做 - 在调试版本中最少


}
//其他方法。我在那里使用ptr1和ptr2
}

main()

一个MyClass(" text1"," text2")
返回0;
}
Hi!
I have a problem with such situation. I have a class like below and have some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor and I free in destructor. For one pointer there is some error when
dustroctor delete objects. Why? What should I have to change?

Please help.

bart

class A
{
char *ptr1;
char *ptr2;
A() {ptr1=NULL; ptr2=NULL;}

A(char *string1, char *string2)
{
ptr1=NULL; ptr2=NULL; // because string1 or string2 might be
NULL??
if (string1 != NULL)
{
ptr1 = new char[strlen(string1)+1];
strcpy(ptr1, string1);
}
if (string2 != NULL)
{
ptr2 = new char[strlen(string2)+1];
strcpy(ptr2, string2);
}
}
virtual ~A()
{
if (ptr1 != NULL)
delete [] this->ptr1;
if (ptr2 != NULL)
delete [] this->ptr2;
ptr1=NULL; ptr2=NULL; // Always do this after delete[] - at
least in debug versions.
}
// other methods. I use there ptr1 and ptr2
}

main()
{

A MyClass("text1", "text2")
return 0;
}




HTH,

Gernot



HTH,
Gernot


bArT写道:
我遇到了这种情况的问题。我有一个类如下,并有一些指针(ptr1和ptr2)。我在构造函数中动态分配内存
我在析构函数中释放。对于一个指针,当
dustroctor删除对象时会出现一些错误。为什么?我该怎么办?


您需要遵守三条规则。请查一下。

请帮忙。

bart

A级
{*> char * ptr1; char * ptr2;
A(char * string1,char * string2)


为了让你的代码运作良好,''string1''和''string2' '这里

应该是''const char *''。

{
if(string1!= NULL)
{
ptr1 = new char [strlen(string1)+1];
strcpy(ptr1,string1);
}
if(string2!= NULL)
{
ptr2 = new char [strlen(string2)+1];
strcpy(ptr2,string2);
}


这一切都很好,有一个例外:如果''string1''是NULL?

''ptr1'得到了什么价值? ''ptr2''相同。在大多数情况下,它们会在它们中产生垃圾,这会导致尝试删除内存中的[]

,这反过来会导致不确定的行为。


确保ptr1和ptr2在正文之前初始化为0:


A(const char * s1,const char * s2):ptr1(0) ,ptr2(0)

{

if(s1 ...

}
虚拟~A()


你_really_需要虚拟吗?

{
if(ptr1!= NULL)
删除[] this-> ptr1;
if(ptr2!= NULL)
delete [] this-> ptr2;


检查是不必要的。''this->''是不必要的。

}
//其他方法。我在那里使用ptr1和ptr2
}


;

main ()


int main()

{

一个MyClass(" text1"," text2")
返回0;
}
I have a problem with such situation. I have a class like below and have
some pointers (ptr1 and ptr2). I dynamically allocate memory in constructor
and I free in destructor. For one pointer there is some error when
dustroctor delete objects. Why? What should I have to change?
You need to adhere to the "Rule of Three". Look it up.

Please help.

bart

class A
{
char *ptr1;
char *ptr2;
A(char *string1, char *string2)
In order for your code to work well, ''string1'' and ''string2'' here
should be ''const char*''.
{
if (string1 != NULL)
{
ptr1 = new char[strlen(string1)+1];
strcpy(ptr1, string1);
}
if (string2 != NULL)
{
ptr2 = new char[strlen(string2)+1];
strcpy(ptr2, string2);
}
That is all fine, with one exception: what if ''string1'' is NULL?
What value does ''ptr1'' get? Same for ''ptr2''. In most cases they
will get garbage in them, which will cause an attempt to delete[]
the memory, which in turn will cause undefined behaviour.

Make sure ptr1 and ptr2 are initialised to 0 before the body:

A(const char* s1, const char* s2) : ptr1(0), ptr2(0)
{
if (s1 ...
}
virtual ~A()
Do you _really_ need it virtual?
{
if (ptr1 != NULL)
delete [] this->ptr1;
if (ptr2 != NULL)
delete [] this->ptr2;
Checking is unnecessary. ''this->'' is unnecessary.
}
// other methods. I use there ptr1 and ptr2
}
;

main()
int main()
{

A MyClass("text1", "text2")
return 0;
}




语法错误s你把你的代码输入到消息

而不是从你的真实项目中复制它。这可能根本就不是b $ b显示出问题的地方。下次发布_real_代码。


Victor



The syntax errors suggest that you typed your code into the message
instead of copying it from your real project. That might simply not
show the place where the problem was. Next time post _real_ code.

Victor


" Gernot Frisch" < Me@Privacy.net>在新闻中写道:2msittFqcot4U1 @ uni-

berlin.de:
"Gernot Frisch" <Me@Privacy.net> wrote in news:2msittFqcot4U1@uni-
berlin.de:

" bArT" < BA ***** @ poczta.onet.pl> schrieb im Newsbeitrag
新闻:ce ********** @ nemesis.news.tpi.pl ...

"bArT" <ba*****@poczta.onet.pl> schrieb im Newsbeitrag
news:ce**********@nemesis.news.tpi.pl...
嗨!
我遇到了问题这种情况。我有一个类如下,
Hi!
I have a problem with such situation. I have a class like below and


一些指针(ptr1和ptr2)。我在
some pointers (ptr1 and ptr2). I dynamically allocate memory in


构造函数


constructor

中动态分配内存,我在析构函数中释放。对于一个指针,当
dustroctor删除对象时会出现一些错误。为什么?我该怎么办?


你应该先告诉我们错误....

A级
{
char * ptr1;
char * ptr2;
and I free in destructor. For one pointer there is some error when
dustroctor delete objects. Why? What should I have to change?
You should first tell us the error....
class A
{
char *ptr1;
char *ptr2;



A(){ptr1 = NULL; ptr2 = NULL;}



A() {ptr1=NULL; ptr2=NULL;}




我应该更喜欢初始化列表到显式赋值。难道没有b $ b为指针做出很多不同,但对于富班来说更重要。

更好地养成这个习惯:


A():ptr1(NULL),ptr2(NULL){};



One should prefer the initializer lists to explicit assignment. Doesn''t
make much of a different for pointers, but matters more for rich classes.
Better to get into the habit:

A() : ptr1(NULL), ptr2(NULL) {};

A(char * string1,char * string2)
{
A(char *string1, char *string2)
{



ptr1 = NULL; PTR2 = NULL; //因为string1或string2可能是
NULL ??



ptr1=NULL; ptr2=NULL; // because string1 or string2 might be
NULL??




与上面相同:


A(char * string1,char * string2):ptr1(NULL),ptr2(NULL)



Same as above:

A(char * string1, char * string2) : ptr1(NULL), ptr2(NULL)

if(string1!= NULL)
{
ptr1 = new char [strlen(string1)+1];
strcpy(ptr1,string1);
}
if(string2!= NULL)
{
ptr2 = new char [strlen(string2)+1];
strcpy(ptr2,string2);
}
}
virtual~A()
{
if(ptr1!= NULL)


此测试没有必要。在NULL

指针上执行删除或删除[]总是安全的(它什么都不做,但它是安全的)。

delete [] this-> ptr1;
if(ptr2!= NULL)
delete [] this-> ptr2;
if (string1 != NULL)
{
ptr1 = new char[strlen(string1)+1];
strcpy(ptr1, string1);
}
if (string2 != NULL)
{
ptr2 = new char[strlen(string2)+1];
strcpy(ptr2, string2);
}
}
virtual ~A()
{
if (ptr1 != NULL)
This test isn''t necessary. Performing a delete or delete[] on a NULL
pointer is always safe (it does nothing, but it''s safe).
delete [] this->ptr1;
if (ptr2 != NULL)
delete [] this->ptr2;



ptr1 = NULL; PTR2 = NULL; //在删除[]后总是这样做 - 至少在调试版本中。



ptr1=NULL; ptr2=NULL; // Always do this after delete[] - at
least in debug versions.

}
//其他方法。我在那里使用ptr1和ptr2
}

main()


int main()


C ++中没有隐式返回类型。

{

A MyClass(" text1"," text2")
返回0;
}
}
// other methods. I use there ptr1 and ptr2
}

main()
int main()

There are no implicit return types in C++.
{

A MyClass("text1", "text2")
return 0;
}




手写,_this_特定程序导致失败?如果没有,请发布显示问题的最短,可编辑的程序。


一个常见的错误是你可能在某处导致的某个地方

要调用的A的复制构造函数,而你的复制构造函数

目前做的不对。作为一般提示,任何时候你都可以在一个类中使用动态分配的内存(或几乎任何其他动态资源)来玩你的b
$需要提供你的

自己的构造函数,复制构造函数,赋值运算符和析构函数。

(或者可能将它们声明为私有而不提供正文你

显然想要禁止复制构造,例如)



Offhand, did _this_ specific program cause a failure? If not, post the
shortest, compilable program that exhibits the problem.

One common error is that somewhere along the line you might be causing
the copy constructor of A to be invoked, and your copy constructor
currently doesn''t do the right thing. As a general hint, anytime you''re
playing with dynamically allocated memory in a class (or pretty much any
other "dynamic" resource), you are likely going to need to supply your
own Constructor, Copy Constructor, Assignment operator, and Destructor.
(Or perhaps declare them as private and don''t supply a body if you
explicitly want to disallow copy construction, for example)


这篇关于析构函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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