Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5849591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:01:20+00:00 2026-05-22T13:01:20+00:00

class Array { double *mx; int mn; public: Array(); ~Array(){delete []mx}; Array& operator-(Array& b);

  • 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;
}

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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T13:01:20+00:00Added an answer on May 22, 2026 at 1:01 pm
    • 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class object array into which I have typecasted many different
I'm working on a class that is storing a 2D array of class MyType
In Java, it would look like this: class Foo { float[] array; } Foo
I have a class that contains an array. I want this array to be
I have a class that contains a dynamically allocated array, say class A {
I have a class 'Data' that uses a getter to access some array. If
Suppose I have an array of a objects of user defined class. Wanted to
I have a vector-like class that contains an array of objects of type T
I have an app holding an array of instances of a class called SwimmingPool.
I'm working on a sparse matrix class that needs to use an array of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.