title: C++ Unidentifiable Memory Leak
body: <p>I have 2 files, one of which is my testing class. My code compiles and executes, but there is still one memory leak concerning my allocating and freeing of my memory. I haven't created any objects personally while writing this code so I am confused as to how a memory leak exists.</p>
  
  <pre><code>#include &lt;algorithm&gt;
  
  
  class sorted_sc_array {
  public:
  
  sorted_sc_array() : size_(0), arraySize(0), ptr_(nullptr) { }
  
  ~sorted_sc_array() { delete[] ptr_; }
  
  
  sorted_sc_array(const sorted_sc_array&amp; A) : size_(A.size_) {
  
    if(size_ == 0) ptr_ = nullptr;
    else {
      delete[] ptr_;
      ptr_ = new signed char[size_];
      std::copy(A.ptr_, A.ptr_ + size_, ptr_);
    }
  
    // int x = A.size_;
    // this -&gt; size_ = 0;
    // for(int i = 0; i &lt; x; i++){
    //   this -&gt; insert(A.data()[i]);
    // }
  }
  
  
  sorted_sc_array&amp; operator=(const sorted_sc_array&amp; A){
  
    if (this ==&amp;A) return *this;
    delete[] ptr_;
    size_ = A.size_;
    if (size_ == 0) ptr_ = nullptr;
    else{
      ptr_ = new signed char[size_];
      std::copy(A.ptr_, A.ptr_ + size_, ptr_);
    }
    return *this;
  
  
    //
    // int x = A.size_;
    // this -&gt; size_ = 0;
    // for(int i = 0; i &lt; x; i++){
    //   this -&gt; insert(A.data()[i]);
    //   }
  
  }
  
  // RETURNS SIZE OF THE ARRAY (i.e. HOW MANY ELEMENTS IT STORES)
  int size() const { return size_; }
  
  // RETURNS RAW POINTER TO THE ACTUAL DATA, CAN BE INVOKED AT ANY TIME
  const signed char* data() const {
     std::sort(ptr_, ptr_ + size_);
     return ptr_;
     delete[] ptr_;
   }
  
  
  
  void insert(signed char c){
  
    if(size() == 0){
      ptr_ = (signed char*)malloc(2*sizeof(char));
      ptr_[0] = c;
      ptr_[1] = 0;
      arraySize = 1;
    } else if(arraySize == size_) {
      ptr_ = (signed char*)realloc(ptr_, (size_ * 2)*sizeof(char));
      arraySize = arraySize * 2;
      ptr_[size_] = c;
    //  ptr_[size_ + 1] = 0;
      //arraySize = arraySize * 2;
  
    }  else {
    //  ptr_ = (signed char*)realloc(ptr_, (size_ + 2)*sizeof(char));
      ptr_[size_] = c;
    //  ptr_[size_ + 1] = 0;
    }
    size_++;
    // std::sort(ptr_, ptr_ + size_);
    }
  
  
  
  
  
  private:
  int size_;         // size of the array
  signed char* ptr_; // pointer to the array
  int arraySize;
  
  }; // class sorted_sc_array
  
  #endif // A2_HPP
  </code></pre>
  
  <p>This is testing the other class:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "a2.hpp"
  
  
  int main(int argc, char* argv[]) {
  sorted_sc_array A;
  
  {
      sorted_sc_array T;
      for (signed char c = -128; c &lt; 127; ++c) T.insert(c);
  
      T = T;
  
      sorted_sc_array V = T;
      A = V;
  }
  
  const auto first = A.data();
  const auto last = first + A.size();
  
  auto size = A.size();
  bool res = std::is_sorted(first, last);
  
  if (!res || (A.size() != 255)) std::cout &lt;&lt; "fail";
  else std::cout &lt;&lt; "pass";
  
  std::cout &lt;&lt; std::endl;
  
  return 0;
  } // main
  </code></pre>
  
  <p>And this is the result from valgrind:</p>
  
  <pre><code>==10205== Memcheck, a memory error detector
  ==10205== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
  ==10205== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
  ==10205== Command: ./a2
  ==10205== 
  ==10205== Conditional jump or move depends on uninitialised value(s)
  ==10205==    at 0x400E35: sorted_sc_array::sorted_sc_array(sorted_sc_array 
  const&amp;) (a2.hpp:30)
  ==10205==    by 0x400BC2: main (a2.cpp:22)
  ==10205== 
  ==10205== Mismatched free() / delete / delete []
  ==10205==    at 0x4C2F74B: operator delete[](void*) (in 
  /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==10205==    by 0x400DF2: sorted_sc_array::~sorted_sc_array() (in 
  /home/vagrant/A2/a2)
  ==10205==    by 0x400BED: main (a2.cpp:17)
  ==10205==  Address 0x5ab6fb0 is 0 bytes inside a block of size 256 alloc'd
  ==10205==    at 0x4C2FD5F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-
  amd64-linux.so)
  ==10205==    by 0x401036: sorted_sc_array::insert(signed char) (a2.hpp:87)
  ==10205==    by 0x400B8A: main (a2.cpp:18)
  ==10205== 
  pass
  ==10205== 
  ==10205== HEAP SUMMARY:
  ==10205==     in use at exit: 72,704 bytes in 1 blocks
  ==10205==   total heap usage: 13 allocs, 12 frees, 74,750 bytes allocated
  ==10205== 
  ==10205== LEAK SUMMARY:
  ==10205==    definitely lost: 0 bytes in 0 blocks
  ==10205==    indirectly lost: 0 bytes in 0 blocks
  ==10205==      possibly lost: 0 bytes in 0 blocks
  ==10205==    still reachable: 72,704 bytes in 1 blocks
  ==10205==         suppressed: 0 bytes in 0 blocks
  ==10205== Reachable blocks (those to which a pointer was found) are not 
  shown.
  ==10205== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  ==10205== 
  ==10205== For counts of detected and suppressed errors, rerun with: -v
  ==10205== Use --track-origins=yes to see where uninitialised values come 
  from
  ==10205== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
  </code></pre>