gpt4 book ai didi

二维数组指针的 C++ 内存泄漏

转载 作者:行者123 更新时间:2023-11-28 03:08:58 25 4
gpt4 key购买 nike

根据 Dr. Memory,以下三个函数在标有“//memory leak vvv”的行中包含内存泄漏。我对 C++ 和指针比较陌生,不确定为什么这些行会导致泄漏。 “values_”是一个 T**,是 UndoArray 类的成员变量。

template <class T> void UndoArray<T>::set(size_type i, const T& v) {
counts_[i]++;
if(!initialized(i)) {
// memory leak vvv
values_[i] = new T[1];
values_[i][0] = v;
} else {
// memory leak vvv
T* temp = new T[counts_[i]];
for(int j = 0; j < counts_[i] - 1; j++) {
temp[j] = values_[i][j];
}
temp[counts_[i] - 1] = v;
delete [] values_[i];
values_[i] = temp;
}
}

template <class T> void UndoArray<T>::undo(size_type i) {
counts_[i]--;
if(counts_[i] == 0) {
values_[i] = NULL;
} else {
T* temp = values_[i];
// memory leak vvv
values_[i] = new T[counts_[i]];
for(int j = 0; j < counts_[i]; j++) {
values_[i][j] = temp[j];
}
delete [] temp;
}
}

template <class T> void UndoArray<T>::copy(const UndoArray<T>& ua) {
size_ = ua.size_;
counts_ = new unsigned[size_];
for(int i = 0; i < size_; i++) {
counts_[i] = ua.counts_[i];
}
values_ = new T*[size_];
for(int i = 0; i < size_; i++) {
if(counts_[i] == 0) {
values_[i] = NULL;
} else {
// memory leak vvv
values_[i] = new T[counts_[i]];
for(int j = 0; j < counts_[i]; j++) {
values_[i][j] = ua.values_[i][j];
}
}
}
}

UndoArray 的构造函数使用...

template <class T> void UndoArray<T>::create() {
size_ = 0;
counts_ = new unsigned[size_];
values_ = new T*[size_];
}

...如果调用默认构造函数(无参数)或...

template <class T> void UndoArray<T>::create(size_type n) {
size_ = n;
counts_ = new unsigned[size_];
for(int i = 0; i < size_; i++)
counts_[i] = 0;
values_ = new T*[size_];
for(int i = 0; i < size_; i++)
values_[i] = NULL;
}

...如果指定了初始数组大小。

析构函数看起来像...

template <class T> UndoArray<T>::~UndoArray() {
delete [] counts_;
if(values_ != NULL) {
for(int i = 0; i < size_; i++) {
delete [] values_[i];
}
}
delete [] values_;
}

最佳答案

代码中有几处不对:

template <class T> void UndoArray<T>::copy(const UndoArray<T>& ua) {
size_ = ua.size_;
counts_ = new unsigned[size_];
for(int i = 0; i < size_; i++) {
counts_[i] = ua.counts_[i];
}
//What if values_ is not null here? You do not delete the old data
values_ = new T*[size_];

而且在您发布的代码中还有一些情况与您类似。

Edit1:给你另一个例子

template <class T> void UndoArray<T>::undo(size_type i) {
counts_[i]--;
if(counts_[i] == 0) {
//what if values_[i] != nullptr here? You will leak the old value...
values_[i] = NULL;

当然,您应该确保在析构函数中删除每个指针。

喜欢:

~UndoArray()
{
if (nullptr != values_)
{
for (int i = 0; i < size_; ++i)
{
delete [] values[i];
}

delete [] values;
}
}

关于二维数组指针的 C++ 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989410/

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