将动态分配的阵列复制到更大的阵列而不会出现内存泄漏

时间:2016-02-19 22:08:52

标签: c++ memory memory-leaks dynamic-arrays dynamic-allocation

我试图将我的两个动态分配的指针数组的大小增加一个,所以我创建临时数组,复制旧值,删除原始数据,然后重新分配它们。代码编译并运行正常,但我得到了内存泄漏,无法弄明白。

我的成员变量是宽度,*高度和**数据。

void Tetris::add_right_column() {

    width += 1;
    int* temp_heights = new int[width];
    char** temp_data = new char*[width];

    // copies old values into the temp one
    for (int i=0; i<width-1; i++) {
        temp_heights[i] = heights[i];
        temp_data[i] = data[i];
    }

    // adds new value into temps
    temp_data[width-1] = new char[0];
    temp_heights[width-1] = 0;

    // deletes original arrays of pointers
    delete [] heights;
    delete [] data;

    // reassigns the pointers
    heights = temp_heights;
    data = temp_data;

}

我正在使用记忆博士并且有一些泄漏:

Error #2: LEAK 28 direct bytes 0x019685c0-0x019685dc + 0 indirect bytes
# 0 replace_operator_new_array               [d:\drmemory_package\common\alloc_replace.c:2928]
# 1 Tetris::add_right_column                 [********/tetris.cpp:308]
# 2 additional_student_tests                 [********/main.cpp:276]
# 3 main                                     [********/main.cpp:41]

Error #3: LEAK 28 direct bytes 0x01968600-0x0196861c + 12 indirect bytes
# 0 replace_operator_new_array               [d:\drmemory_package\common\alloc_replace.c:2928]
# 1 Tetris::add_right_column                 [********/tetris.cpp:309]
# 2 additional_student_tests                 [********/main.cpp:276]
# 3 main                                     [********/main.cpp:41]

我所知道的是我在这两行分配的内存:

int* temp_heights = new int[width];
char** temp_data = new char*[width];

未正确释放。我试过了:

delete [] temp_heights;
delete [] temp_data;

// after copying data[i] over to temp_data[i]
for (int i=0; i<width; i++) {
    delete data[i];
}

但这两个都导致它停止工作。我知道我不能使用reallocate因为我正在使用new。在这种情况下,对我来说避免内存泄漏的最佳方法是什么?

编辑:

我的构造函数:

Tetris::Tetris(int given_width) {
    width = given_width;
    heights = new int[width]; 
    // sets n (width) number of heights = to 0
    for (int i =0; i<width; i++) {
        heights[i] = 0;
    }
    // same type as the type of the arrays it points to
    data = new char*[width];
    for (int i=0; i<width; i++) {
        data[i] = new char[heights[i]];
    }
}

我的析构函数:

void Tetris::destroy() {
    delete [] data;
    delete [] heights;
 }

我的课程:

class Tetris {
public:

    // CONSTRUCTORS
    Tetris(int given_width);

    // ACCESSORS
    int get_width() const { return width;};
    int get_max_height() const;
    int count_squares() const;
    void print() const;
    void destroy();
    int count_squares();

    // MODIFIERS
    void add_piece(char piece, int rotation, int position);
    int remove_full_rows();
    void add_left_column() { };
    void add_right_column();
    void remove_right_column() { };
    void remove_left_column() { };

private:
    // MEMBER VARIABLES
    int width;
    int *heights;
    char **data;
};

主要的例子:

void main() {
    Tetris tetris(6);
    tetris.print();
    tetris.add_right_column();
    tetris.print();
}

1 个答案:

答案 0 :(得分:1)

您的data []包含每次在堆中分配的char *数组。

在删除[]数据之前,请确保删除所有&#34;新字符&#34;你的数据数组指出。