c++类数组的神秘传递

发布于 2024-11-09 01:21:25 字数 1403 浏览 0 评论 0原文

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" 

http:// /s41.radikal.ru/i091/1105/e1/2349397c04a2.png

我不明白为什么会发生这种情况。

据我了解,当我调用 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" 

http://s41.radikal.ru/i091/1105/e1/2349397c04a2.png

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨洒年华 2024-11-16 01:21:25
Array Array::operator -(Array b)

此行将创建数组的副本。由于您没有复制构造函数,编译器只会复制所有字段,包括指针字段“mx”。现在您有两个对象都指向相同的分配内存。当每个被破坏时,将调用delete []......

您需要编写一个复制构造函数或确保不发生复制。
要做到这一点,请通过引用传递

Array Array::operator -(Array& b)

(这可能也应该是 const...但这是一个不同的问题)

Array Array::operator -(Array b)

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

Array Array::operator -(Array& b)

(That should probably be const too... but that's a different issue)

跨年 2024-11-16 01:21:25

您违反了三规则

You violated the rule of three.

忆离笙 2024-11-16 01:21:25
  • operator- 应该引用,否则您将执行不必要的复制。然而,它并不需要这样做。它当然应该返回一个值,因为 - 在语义上为您提供了一个新对象。当您编写 c = ab 时,您不会期望 ab 发生变化。
  • 如上所述,您不需要需要将引用引入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 write c = a-b, you don't expect a or b to change.
  • As noted above, you don't need to take a reference into operator-, and in your second example you take by value. This is OK, except you have a second bug:
    • Your Array class has an internal buffer that it news on construction, and deletes when it gets destroyed (~Array).
    • However, it does not have a user-defined copy constructor, and the buffer is not automatically copied for you; only the pointer mx is copied.
    • So, when you copy the Array, you now have two objects with a pointer mx pointing to the same buffer. When one copy goes out of scope, that buffer is deleted; some time later, the other copy tries to do the same, and deleteing the same buffer twice is an error.

My suggestions:

  • Write a copy constructor and operator= into your class Array. Very important.
  • Have operator- take a reference anyway. It makes mores sense.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文