0

I am doing some OOP problems and I was provided this code

int main() {
 IntegerArray a(2);//This call the constructor
 a.data[0] = 4; a.data[1] = 2;
 if (true) {
 IntegerArray b = a;
 }
 cout << a.data[0] << endl; // The result is 4
}

I add the indentation to the code and created the class which contains two constructors and a destructor

class IntegerArray
{
    public:
        int *data;
        IntegerArray()
        {
            data = new int[1];
        }
        IntegerArray(int len)
        {
            data = new int[len];
        }

        ~IntegerArray()
        {
            delete data;
        }
};


int main() 
{
    IntegerArray a(2);//This call the constructor
    
    a.data[0] = 4;
    a.data[1] = 2;
    
    if (true) 
        IntegerArray b = a;
    
    printf("%d\n", a.data[0]);
}

The question is why does this code cause a core dump in IntegerArray b = a isn't it copying the object a into a new object b, I guess this is my understanding of Exercise 3

This is the program output

0
free(): double free detected in tcache 2
Aborted (core dumped)
loaded_dypper
  • 262
  • 3
  • 12
  • 2
    Search for "C++ rule of three". – Thomas Aug 16 '22 at 19:24
  • What do you suppose happens in the line `IntegerArray b = a;`, do you have any idea so far? – user253751 Aug 16 '22 at 19:25
  • @user253751 I am assuming that the object `a` is being copied into `b` hence the `free detected` but i am not sure – loaded_dypper Aug 16 '22 at 19:28
  • 2
    @loaded_dypper -- Look at the duplicate link, and go to the section that says **Managing resources** -- right there is a perfect example of the mistake you are making. – PaulMcKenzie Aug 16 '22 at 19:28
  • @loaded_dypper focus on what `~IntegerArray()` does when `data` has been copied to multiple objects. – Drew Dormann Aug 16 '22 at 19:34
  • `new`/`delete`, `new []`/`delete []` – crashmstr Aug 16 '22 at 19:35
  • @DrewDormann isn't the data copied in `IntegerArray`? – loaded_dypper Aug 16 '22 at 19:36
  • @loaded_dypper `IntegerArray` contains **a single pointer**. The pointer is copied. Both objects have the same pointer. Both objects try to `delete` the same pointer. That causes a core dump. You can learn much more detail about this by [reading the duplicate](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – Drew Dormann Aug 16 '22 at 19:38
  • @PaulMcKenzie I can't seem to figure out how to make a copy assignment operator, could you please elaborate how to do so – loaded_dypper Aug 16 '22 at 19:44
  • @loaded_dypper Doesn't the solution at the link you posted show you the way to do this? – PaulMcKenzie Aug 16 '22 at 19:46
  • @PaulMcKenzie it does but i don't seem to get the full picture – loaded_dypper Aug 16 '22 at 19:47
  • @loaded_dypper you may have a new [question to ask](https://stackoverflow.com/questions/ask). Asking many questions in long comment chains will have limited visibility to people who might answer you as well as future visitors with the same questions. – Drew Dormann Aug 16 '22 at 19:49
  • 1
    @loaded_dypper Yes, data is a pointer and so the pointer gets copied. Now both a.data and b.data contain the same address (because it was copied). So what happens when ~IntegerArray runs for b - it destroys the data, right? – user253751 Aug 16 '22 at 19:51
  • 1
    an important thing to know is that a.data isn't the data, just a pointer to the data (== a variable that stores the address of where the data is) – user253751 Aug 16 '22 at 19:58
  • 1
    @loaded_dypper You should also know that if you replaced `int *data;` with `std::vector data;` all of this work will already be written for you. Your class will _just work_ and won't need a user-defined destructor anymore. – Drew Dormann Aug 16 '22 at 20:11
  • @DrewDormann this is good to know but i am learning the basics before using other header files – loaded_dypper Aug 17 '22 at 04:47
  • @loaded_dypper -- It could be said that `std::vector` *is* the "basics", and what you are attempting to do is more advanced. – PaulMcKenzie Aug 17 '22 at 07:56

0 Answers0