gpt4 book ai didi

C++ 内存泄漏

转载 作者:行者123 更新时间:2023-11-30 02:29:07 26 4
gpt4 key购买 nike

我有 2 个文件,其中一个是我的测试类。我的代码编译并执行,但我无法消除内存泄漏。我整天都在尝试,并且越来越近,但我被卡住了!我使用了 valgrind,它显示内存泄漏,但我不确定如何修复它们。我的代码没有创建单个对象,所以我很困惑。请帮忙!

#ifndef A2_HPP
#define A2_HPP

#include <algorithm>

class sorted_sc_array {
public:

/*
* return: none
* constructor with no argument assign size_ = 0 and ptr_ to null pointer
*/
sorted_sc_array() : size_(0), ptr_(nullptr) {
}

/*
* return: none
* destructor delete the pointer ptr_
*/
~sorted_sc_array() {
delete[] ptr_;
}

/*
* return: none
* when assign an object to new object
*/
sorted_sc_array(const sorted_sc_array& A){
int sz = A.size_;
this->size_ = 0;
for(int i = 0; i < sz; i++) this->insert(A.data()[i]);
}

/*
* return: sorted_sc_array
* overloading of operator =
*/
sorted_sc_array& operator=(const sorted_sc_array& A){
int sz = A.size_;
this->size_ = 0;
for(int i = 0; i < sz; i++) this->insert(A.data()[i]);;
}

/*
* return int
* return the size of the ptr_
*/
int size() const {
return size_;
}

/*
* return char*
* return the deta stored in the pointer ptr_
*/
const signed char* data() const {
return ptr_;
}

/*
* return void
* add new char to the pointer ptr_ and sort the the new string after addition
*/
void insert(signed char c) {
if(size_ == 0){
ptr_ = (signed char*)malloc(2*sizeof(char));
ptr_[0] = c;
ptr_[1] = '\0';
}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

}; // class sorted_sc_array

#endif // A2_HPP

这是在测试其他类

/*
* File: a2.pp
* Description: testing class 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 得到的:

    ==8291== Memcheck, a memory error detector
==8291== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==8291== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==8291== Command: ./a2
==8291==
==8291== Mismatched free() / delete / delete []
==8291== at 0x402ECB8: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x8048A39: main (in /home/jay/A2/a2)
==8291== Address 0x4416480 is 0 bytes inside a block of size 256 alloc'd
==8291== at 0x402F2CC: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x80488D6: main (in /home/jay/A2/a2)
==8291==
==8291== Mismatched free() / delete / delete []
==8291== at 0x402ECB8: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x8048A42: main (in /home/jay/A2/a2)
==8291== Address 0x440b0b8 is 0 bytes inside a block of size 256 alloc'd
==8291== at 0x402F2CC: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x8048822: main (in /home/jay/A2/a2)
==8291==
pass
==8291== Mismatched free() / delete / delete []
==8291== at 0x402ECB8: operator delete[](void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x8048AAF: main (in /home/jay/A2/a2)
==8291== Address 0x4421848 is 0 bytes inside a block of size 256 alloc'd
==8291== at 0x402F2CC: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x804899A: main (in /home/jay/A2/a2)
==8291==
==8291==
==8291== HEAP SUMMARY:
==8291== in use at exit: 19,200 bytes in 2 blocks
==8291== total heap usage: 1,022 allocs, 1,020 frees, 151,548 bytes allocated
==8291==
==8291== 256 bytes in 1 blocks are definitely lost in loss record 1 of 2
==8291== at 0x402F2CC: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==8291== by 0x804874C: main (in /home/jay/A2/a2)
==8291==
==8291== LEAK SUMMARY:
==8291== definitely lost: 256 bytes in 1 blocks
==8291== indirectly lost: 0 bytes in 0 blocks
==8291== possibly lost: 0 bytes in 0 blocks
==8291== still reachable: 18,944 bytes in 1 blocks
==8291== suppressed: 0 bytes in 0 blocks
==8291== Reachable blocks (those to which a pointer was found) are not shown.
==8291== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==8291==
==8291== For counts of detected and suppressed errors, rerun with: -v
==8291== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

最佳答案

您的 operator=size_ 设置为 0。

size_ 为 0 时,您的 insert() 分配一个新数组,并将 ptr_ 设置给它,泄漏先前分配的数组在 _ptr 中。

您的赋值运算符还需要显式删除 _ptr,然后在启动复制之前将其设置为nullptr

而且您仍在对使用 malloc 的数组使用 delete。这是未定义的行为,也是另一个错误。

关于C++ 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39949504/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com