gpt4 book ai didi

c++ - 堆上二维数组的内存泄漏

转载 作者:行者123 更新时间:2023-12-02 19:41:55 45 4
gpt4 key购买 nike

我所创建的类存在大量内存泄漏问题。该作业需要在堆上创建一个单词搜索谜题。我已经创建了析构函数、复制构造函数并重载了赋值运算符。

我认为这些函数之一一定有问题,因为确保它正常工作的最后检查是在循环中创建对象,看看它是否失败以及我的函数是否崩溃。我尝试过不同形式的析构函数,也尝试过更改复制和赋值运算符,但没有成功。有点不知所措,而且缺乏警告确实使得在没有正确理解堆的情况下进行调试变得困难。

任何帮助将不胜感激!

以下是一些使用堆的函数。

JumblePuzzle::~JumblePuzzle(){
for (int i = 0; i < size; ++i){
delete jumble[i];
}
delete jumble;

}

JumblePuzzle::JumblePuzzle(string word, string diff){
int i = 0;
toHide = word;
difficulty = diff;
jumble = buildArray();
fillArray();
hideWord();
}

JumblePuzzle::JumblePuzzle(JumblePuzzle& temp){
size = temp.size;
rowPos = temp.rowPos;
colPos = temp.colPos;
direction = temp.direction;
toHide = temp.toHide;
difficulty = temp.difficulty;
jumble = temp.getJumble();
}

JumblePuzzle& JumblePuzzle::operator=(const JumblePuzzle& right){
if (this != &right){
for (int i = 0; i < size; ++i){
delete jumble[i];
}
delete[] jumble;
size = right.size;
rowPos = right.rowPos;
colPos = right.colPos;
direction = right.direction;
toHide = right.toHide;
difficulty = right.difficulty;
jumble = right.getJumble();
}
return *this;
}

charArrayPtr* JumblePuzzle::buildArray() const{
charArrayPtr* array = new char*[size];
for (int i = 0; i < size; ++i){
array[i] = new char[size];
}
return array;
}

这是失败的线路。

int loopLimit =20;
for (int i = 0; i < loopLimit; i++)
JumblePuzzle jp("HIDDENWORD", "hard");

感谢您提供任何可能的帮助!

编辑:

这也是我的 .h 文件。

#ifndef JUMBLE_H_
#define JUMBLE_H_

#include <time.h>
#include <cstdlib>
#include <string>
using namespace std;
typedef char* charArrayPtr;


class BadJumbleException {
public:
BadJumbleException(const string&);
string& what();
private:
string message;
};

class JumblePuzzle{
public:
JumblePuzzle(string, string); //simple constructor
JumblePuzzle(JumblePuzzle&); //copy constructor
~JumblePuzzle(); //deconstructor
charArrayPtr* getJumble() const;
JumblePuzzle& operator=(const JumblePuzzle&);

//accessors
int getSize();
int getRowPos();
int getColPos();
char getDirection();

private:
//attributes
int size;
int rowPos;
int colPos;
char direction;
charArrayPtr* jumble;
string toHide;
string difficulty;

void fillArray();
void hideWord();
char randomDirection();
int randomNum(int);
charArrayPtr* buildArray() const;
};
#endif

还有我的 getJumble。它用于创建实际的单词搜索。返回一个拷贝而不是指针,因此无法修改。

charArrayPtr* JumblePuzzle::getJumble() const{

charArrayPtr* tempJumble = new char*[size];
for (int i = 0; i < size; ++i){
tempJumble[i] = new char[size];
}

for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
tempJumble[i][j] = jumble[i][j];
}
}

return tempJumble;
}

最佳答案

您的代码存在一个主要问题,那就是您未能在 JumblePuzzle(string, string) 构造函数中初始化“size”成员。

您还应该做其他事情:

1) 创建一个单独的函数来销毁 JumblePuzzle 类中的二维数组。您似乎正在复制相同的循环以在多个地方执行此操作。如果您只是调用一个函数来完成这项工作,则不需要这样做。

2) 您的赋值和复制构造函数不是异常安全的。如果 new[] 在创建拷贝期间抛出异常,则原始对象具有无效数据。换句话说,你已经破坏了数据,当你想创建另一个二维数组时,当 new[] 说“哎呀”时,你已经破坏了原始数据并且无法将其恢复。

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

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