c++类数组的神秘传递
class Array
{
double *mx; int mn;
public:
Array();
~Array(){delete []mx};
Array& operator-(Array& b); //first right way
Array operator -(Array b); //wrong way, but I don't understand why
};
Array::Array ()
{
mn=10;
mx=new double[mn];
}
//first, works perfectly
Array& Array::operator -(Array& b)
{
int i=0;
for(i=0;i<mn ;i++)
this->mx[i]-=b.mx[i];
return *this;
}
// here is Error
Array Array::operator -(Array b)
{
int i=0;
for(i=0;i<mn ;i++)
this->mx[i]-=b.mx[i];
}
int main() {
Array x,b;
x=x-b;
}
如果我使用第一个重载,一切正常。
但是如果我使用第二个,所有编译都很好,但是当程序执行时,我收到很多这样的错误:
"c++ ** glibc detected *** double free or corruption"
我不明白为什么会发生这种情况。
据我了解,当我调用 Array Array::operator-(Array b) 时,必须复制对象并且一切都必须很好,但是出现错误。
好吧,我读到我必须反对在内存中的同一位置分配的对象。但我尝试这样做:
Array Array::operator +(Array b)
{ Array c;
int i=0;
for(i=0;i<mn;i++)
this->mx[i]+=b.mx[i];
cout<<&this->mx<<" "<<&b.mx<<endl;
exit(0);
return c; }
我期望在内存中收到相同的地址....
答案是 0xbfb45188 0xbfb45178 为什么它们相等?
此外,当我在这里声明类名(一个对象)时
编译器必须在堆栈中为对象提供新的内存 我哪里错了?我不明白....
class Array
{
double *mx; int mn;
public:
Array();
~Array(){delete []mx};
Array& operator-(Array& b); //first right way
Array operator -(Array b); //wrong way, but I don't understand why
};
Array::Array ()
{
mn=10;
mx=new double[mn];
}
//first, works perfectly
Array& Array::operator -(Array& b)
{
int i=0;
for(i=0;i<mn ;i++)
this->mx[i]-=b.mx[i];
return *this;
}
// here is Error
Array Array::operator -(Array b)
{
int i=0;
for(i=0;i<mn ;i++)
this->mx[i]-=b.mx[i];
}
int main() {
Array x,b;
x=x-b;
}
If I use the first overload , all works right.
But if I use the second, all is compiled well, but when program is executed, i receive many errors like this:
"c++ ** glibc detected *** double free or corruption"
I can't figure out why this occurs.
As I understand, when I call Array Array::operator-(Array b)
, the object must be copied and all must be well, but there is error.
well i've read that i've to object that are allocated at the same place in the memory. but i've tried to do this:
Array Array::operator +(Array b)
{ Array c;
int i=0;
for(i=0;i<mn;i++)
this->mx[i]+=b.mx[i];
cout<<&this->mx<<" "<<&b.mx<<endl;
exit(0);
return c; }
i 've expected to receive same addresses in memory....
answer is 0xbfb45188 0xbfb45178 why are they equal?
furhermore, when i declare here name of class(A object)
compiler must give a new memory in stack for object
where am i wrong? i dont understand....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此行将创建数组的副本。由于您没有复制构造函数,编译器只会复制所有字段,包括指针字段“mx”。现在您有两个对象都指向相同的分配内存。当每个被破坏时,将调用delete []......
您需要编写一个复制构造函数或确保不发生复制。
要做到这一点,请通过引用传递
(这可能也应该是 const...但这是一个不同的问题)
This line will create a copy of your array. As you don't have a copy constructor the compiler will just make a copy of all the fields including the pointer field "mx". Now you have two objects both pointing to the same allocated memory. When each one is destructed the delete [] will be called....
You need to either write a copy constructor or ensure that no copying takes place.
To do that pass by reference
(That should probably be const too... but that's a different issue)
您违反了三规则。
You violated the rule of three.
operator-
应该引用,否则您将执行不必要的复制。然而,它并不需要这样做。它当然应该返回一个值,因为-
在语义上为您提供了一个新对象。当您编写c = ab
时,您不会期望a
或b
发生变化。operator-
,并且在第二个示例中您按值获取。这没问题,除非你有第二个错误:Array
类有一个内部缓冲区,它在构造时新建
,并在销毁时删除
(~数组
)。mx
。Array
时,您现在拥有两个对象,其指针mx
指向同一个缓冲区。当一个副本超出范围时,该缓冲区将被删除
;一段时间后,另一个副本尝试执行相同的操作,并且删除
同一缓冲区两次是一个错误。我的建议:
Array
中编写一个复制构造函数和operator=
。非常重要。operator-
获取参考。这很有意义。希望有帮助。
operator-
should take a reference, otherwise you're performing needless copies. However, it doesn't need to. It certainly should return a value, because a-
semantically gives you a new object. When you writec = a-b
, you don't expecta
orb
to change.operator-
, and in your second example you take by value. This is OK, except you have a second bug:Array
class has an internal buffer that itnew
s on construction, anddelete
s when it gets destroyed (~Array
).mx
is copied.Array
, you now have two objects with a pointermx
pointing to the same buffer. When one copy goes out of scope, that buffer isdelete
d; some time later, the other copy tries to do the same, anddelete
ing the same buffer twice is an error.My suggestions:
operator=
into your classArray
. Very important.operator-
take a reference anyway. It makes mores sense.Hope that helps.