C ++无法识别的内存泄漏

时间:2018-03-11 22:42:47

标签: c++

我有2个文件,其中一个是我的测试类。我的代码编译并执行,但是仍有一个关于我的内存分配和释放的内存泄漏。在编写此代码时,我没有亲自创建任何对象,因此我对内存泄漏的存在感到困惑。

#include <algorithm>


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& 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 -> size_ = 0;
  // for(int i = 0; i < x; i++){
  //   this -> insert(A.data()[i]);
  // }
}


sorted_sc_array& operator=(const sorted_sc_array& A){

  if (this ==&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 -> size_ = 0;
  // for(int i = 0; i < x; i++){
  //   this -> 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

这是测试其他课程:

#include <iostream>
#include "a2.hpp"


int main(int argc, char* argv[]) {
sorted_sc_array A;

{
    sorted_sc_array T;
    for (signed char c = -128; c < 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 << "fail";
else std::cout << "pass";

std::cout << std::endl;

return 0;
} // main

这是valgrind的结果:

==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&) (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)

1 个答案:

答案 0 :(得分:2)

查看你的valgrind输出:它没有抱怨内存泄漏,它抱怨未初始化的内存(你的delete复制构造函数中未初始化的指针)以及malloc与{不匹配} {1}}(您需要与分配器保持一致)。

删除有问题的delete[]并使用delete []代替new []malloc