0

i couldn't find my answer with googling so im asking here : Assume that we have code like this and we don't want to overload the copy constructor

 #ifndef ARRAY_H
 #define ARRAY_H

 class Array { 
      public:
      Array(int = 10);

      int* ptr; 
      int size;
               }
 #endif // ARRAY_H

and we have:

#include <iostream>
#include "Array.h"
using namespace std;

Array::Array(int A)
 {
    size = (A > 0) ? A : 10;
    ptr = new int[size];

    for(int i=0; i<size; i++)
     { ptr[i] = i;}
 }

and main() is :

   Array newArray1(10); 
   Array newArray2(8);
   newArray2 = newArray1;

now our professor said we have dangle problem here because we have same address for both newArray1 and newArray2
my question is if we delete newArray1 memory : have we memory leak except dangle??? what happened to newArray2's ptr's memory which ptr was pointing to it before getting newArray1 address??? is this part of memory exist now ??? and have we other memory leaks problem ???

Arian
  • 13
  • 4
  • 1
    Beside the fact that you never call delete[] you also do a shallow copy so yes, there are alot of problems with that code. In order to find out how many memory leaks you have, count number of calls to (x = new[] - delete[]). So assuming int is 4 bytes, 72 bytes never got cleaned up. –  Jun 04 '16 at 14:22
  • thanks Dr.jones its a good formula!!! – Arian Jun 04 '16 at 14:34
  • Dr.jones it seems my question was not straight enough i asked about the newArray2(8) memory, i edited my question but thanks your answer give me what i wanted – Arian Jun 04 '16 at 14:50
  • Well newArray2's ptr will point to newArray1's ptr after operator=. So even if you called delete[] in the destructor both objects would try to deallocate the same memory region and newArray2's ptr would never get deallocated. In order to fix this problem you have to add your own copy constructor and either make a deep copy and/or deallocated newArray2'ptr in there in order to prevent memory leak depending on what kind of behavior you are after. –  Jun 04 '16 at 15:01
  • your well and detailed answer is about the dangle problem, i was speaking about newArray2's ptr's [memory] before newArray2 = newArray1; i was wondering after newArray2 = newArray1;that we loose pointer to last newArray2's ptr's [memory], that part of [memory] remains and we have leak or C++ will handle it, sorry its still confusing its better on paper with figures – Arian Jun 04 '16 at 15:42

1 Answers1

0

To answer your question: You have no destructor delete[]ing the memory you allocate to ptr in the constructor. So yes, you have a leak.

A much better option would be to simply use a std::unique_ptr rather than manual memory management.

Also, why are you not using the constructors initialization list but instead assigning to members in the constructor body?

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
  • Jesper Juhl yes you are right this is not a standard code and it has a lot of mistakes because its not for using in real project i just wanted to know what happens if we dont use copy constructor overloading. – Arian Jun 04 '16 at 14:29
  • @Arian If you agree that I'm right then you should accept the answer rather than posting a comment ;) – Jesper Juhl Jun 04 '16 at 14:30