我从以下代码中获取内存泄漏,我不完全确定原因。我正在使用valgrind来识别这些泄漏但我无法弄清楚如何修复它们。我从使用malloc / free转到new / delete并修复了许多已存在的问题,但现在我有内存泄漏错误。请帮我识别泄漏谢谢。
#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 {
ptr_ = new signed char[size_];
std::copy(A.ptr_, A.ptr_ + size_, ptr_);
}
}
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;
}
// 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_;
}
void insert(signed char c){
if(size() == 0){
ptr_ = new signed char[2*sizeof(char)];
ptr_[0] = c;
ptr_[1] = 0;
arraySize = 1;
} else if(arraySize == size_) {
signed char tempPtr[size_*2];
for(int i = 0;i < size_; i++){
tempPtr[i] = ptr_[i];
}
ptr_ = new signed char[(size_*2)*sizeof(char)];
for(int i = 0;i < size_; i++){
ptr_[i] = tempPtr[i];
}
arraySize = arraySize * 2;
ptr_[size_] = c;
} else {
ptr_[size_] = c;
}
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输出:
==5054== HEAP SUMMARY:
==5054== in use at exit: 72,960 bytes in 9 blocks
==5054== total heap usage: 13 allocs, 4 frees, 74,750 bytes allocated
==5054==
==5054== Searching for pointers to 9 not-freed blocks
==5054== Checked 107,120 bytes
==5054==
==5054== 2 bytes in 1 blocks are definitely lost in loss record 1 of 3
==5054== at 0x4C2E80F: operator new[](unsigned long) (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5054== by 0x4009C0: main (in /home/vagrant/A2/a2)
==5054==
==5054== 254 bytes in 7 blocks are definitely lost in loss record 2 of 3
==5054== at 0x4C2E80F: operator new[](unsigned long) (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==5054== by 0x400A14: main (in /home/vagrant/A2/a2)
==5054==
==5054== LEAK SUMMARY:
==5054== definitely lost: 256 bytes in 8 blocks
==5054== indirectly lost: 0 bytes in 0 blocks
==5054== possibly lost: 0 bytes in 0 blocks
==5054== still reachable: 72,704 bytes in 1 blocks
==5054== suppressed: 0 bytes in 0 blocks
==5054== Reachable blocks (those to which a pointer was found) are not
shown.
==5054== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==5054==
==5054== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
==5054== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)