简体   繁体   English

如何在我的大小调整数组方法中解决此内存泄漏?

[英]How do I fix this memory leak in my resize array method?

I've created a simple integer array class: 我创建了一个简单的整数数组类:

#ifndef INTARRAY_H
#define INTARRAY_H

class IntArray
{
public:
    // Constructors / Destructor
    IntArray();
    IntArray(int size);
    IntArray(const IntArray& rhs);
    ~IntArray();

    // Methods
    int size();
    void resize(int newSize);
    void print();

    //Operator Overloads
    IntArray& operator=(const IntArray& rhs);
    int& operator[](int i);

private:
    // Data Members
    int* mArray;
    int mSize;
};

#endif

Now the issue is in the resize() method. 现在问题出在resize()方法中。 How do I know this? 我怎么知道 Because I don't have any issues with any object until I create an object and use the resize() method. 因为在创建对象并使用resize()方法之前,我对任何对象都没有任何问题。 When I run through the debugger, Visual Studio tells me I have some kind of pointer error once it goes through the destructor for the object that uses the resize function, and that object only. 当我运行调试器时,Visual Studio告诉我,一旦它通过了使用resize函数的对象以及仅该对象的析构函数,就会遇到某种指针错误。 All other objects run through the destructor fine. 所有其他对象都可以通过析构函数运行。 Also, when I'm in the debugger and I delve into what's going on in the resize method, when I delete mArray inside, it does some funky stuff. 另外,当我进入调试器并深入研究resize方法的情况时,当我删除内部的mArray时,它会做一些时髦的事情。 The thing is, it displays on the screen exactly how it should, I just get an error message. 事实是,它准确地显示在屏幕上,我只是收到一条错误消息。 Anyways, this is what my resize() method is and I put in some comments to help you understand what I was thinking I was doing as I implemented the code: 无论如何,这就是我的resize()方法的含义,我添加了一些注释来帮助您了解实现代码时我的想法:

EDIT: Added whole class for people who were asking 编辑:为正在问的人增加了全班

#include <iostream>
#include "IntArray.h"
using namespace std;

// Constructors / Destructor
IntArray::IntArray()
{
    mArray = 0;
    mSize = 0;
}

IntArray::IntArray(int size)
{
    mSize = size;
    if (mSize == 0)
        mArray = 0;
    else
        mArray = new int[mSize];
}

IntArray::IntArray(const IntArray& rhs)
{
    mArray = 0;
    *this = rhs;
}

IntArray::~IntArray()
{
    delete[] mArray;
    mArray = 0;
}

// Methods
int IntArray::size()
{
    return mSize;
}

void IntArray::resize(int size, int newSize)
{

    int* temp = new int[mSize];  //Create temp dynamic array to store the values

    for (int i = 0; i < mSize; i++)  //Copy values from mArray into the temp array
    {
        temp[i] = mArray[i];
    }
    delete[] mArray; //Delete the old array
    mArray = 0;
    mArray = new int(newSize);  //Create a new array with the new size

    if (mSize <= newSize)  //Copy the values from the temp array into the new array
    {
        for (int i = 0; i < mSize; i++)
        {
            mArray[i] = temp[i];
        }
    }
    else
    {
        for (int i = 0; i < newSize; i++)
        {
            mArray[i] = temp[i];
        }
    }
    delete[] temp;  //Delete the temp array
}  //Doesn't work :(

void IntArray::print()
{
    //cout << 
}

// Operator Overloads
IntArray& IntArray::operator=(const IntArray& rhs)
{
    if (this == &rhs)
        return *this;

    delete[] mArray;

    mSize = rhs.mSize;

    mArray = new int[mSize];

    for (int i = 0; i < mSize; i++)
    {
        mArray[i] = rhs.mArray[i];
    }

    return *this;
}

int& IntArray::operator[](int i)
{
    return mArray[i];
}

Thanks for any help. 谢谢你的帮助。 EDIT2: Yes, I know a vector is the exact same thing that I'm doing. EDIT2:是的,我知道矢量与我正在做的完全相同。 That's not the point. 那不是重点。 I'm simply trying to understand pointers and dynamic memory inside of classes better and this is an exercise out of a book that simply gave me the header file code and asked me to implement it. 我只是想更好地理解类内部的指针和动态内存,这是一本书的练习,只是给了我头文件代码并要求我实现它。

Your resize function doesn't set mSize , so later operations probably run into trouble. 您的resize函数未设置mSize ,因此以后的操作可能会遇到麻烦。

Here's a better implementation 这是一个更好的实现

void IntArray::resize(int newSize)
{

    if (newSize == 0)
    {
        mSize = 0;
        delete[] mArray;
        mArray = 0;
    }
    else
    {
        int *newArray = new int [newSize]:
        for (int i = 0; (i < newSize) && (i < mSize); ++i)
            newArray[i] = mArray [i];
        delete[] mArray;
        mArray = newArray;
        mSize = newSize;
    }
}

Try this: 尝试这个:

void IntArray::resize(int newSize)
{
    int* temp = new int[newSize];  //Create temp dynamic array to store the values
    memcpy(temp, mArray, sizeof(int)*(mSize > newSize ? newSize : mSize));
    delete[] mArray;
    mSize = newSize;
    mArray = temp;
}

But you can have memory leaks if there will be exceptions. 但是,如果有异常,您可能会发生内存泄漏。 Using vector is better. 使用向量更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM