C ++内存泄漏 - 我删除什么,在哪里?

时间:2011-05-13 15:45:06

标签: c++ pointers memory-management memory-leaks

以下功能中存在内存泄漏。我遇到的麻烦是知道删除的方式,时间,地点和内容。这是代码:

#include "stdafx.h"
#include <iostream>

void someFunc(double** ppDoubleArray, int length)
{
    double* pNewDoubleArray = new double[length];

    for(int i = 0; i < length; i++)
    {
        pNewDoubleArray[i] = 3 * i + 2;
    }

    *ppDoubleArray = pNewDoubleArray;
}
int main()
{
    double dbls[] = { 1, 2, 3, 4, 5 };
    double* pArray = dbls;

    int length = sizeof dbls / sizeof dbls[0];

    std::cout << "Before..." << std::endl;

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    someFunc(&pArray, length);

    std::cout << "After..." << std::endl;

    //Expected series is: 2, 5, 8, 11, 14

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    while(true){ }

    return 0;
}

正如您所看到的,我尝试删除使用它后分配的新数组。这实际上是有道理的,这不起作用,但我不知道该怎么做..

添加了delete[] pArray

#include "stdafx.h"
#include <iostream>

void someFunc(double** ppDoubleArray, int length)
{
    double* pNewDoubleArray = new double[length];

    for(int i = 0; i < length; i++)
    {
        pNewDoubleArray[i] = 3 * i + 2;
    }

    *ppDoubleArray = pNewDoubleArray;
}
int main()
{
    double dbls[] = { 1, 2, 3, 4, 5 };
    double* pArray = dbls;

    int length = sizeof dbls / sizeof dbls[0];

    std::cout << "Before..." << std::endl;

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    someFunc(&pArray, length);

    std::cout << "After..." << std::endl;

    //Expected series is: 2, 5, 8, 11, 14

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    delete[] pArray;

    std::cout << std::endl;

    while(true){ }

    return 0;
}

如果在这种情况下所有内存泄漏,这是否会解决?

5 个答案:

答案 0 :(得分:6)

你正在分配&amp;删除函数中的数组。你也回来了。

int main()
{

//这个在堆栈上分配,因此在退出main()

时将被删除
    double dbls[] = { 1, 2, 3, 4, 5 };

    double* pArray = dbls;

    //...

//你的函数分配了pArray现在指向的一些内存

    someFunc(&pArray, length);

    std::cout << "After..." << std::endl;

    //Expected series is: 2, 5, 8, 11, 14

    for(int i = 0; i < length; i++)
    {
        std::cout << pArray[i] << ", ";
    }

    std::cout << std::endl;

    while(true){ }

//忘记删除功能分配的内存!内存泄漏!!!

    return 0;
}

答案 1 :(得分:1)

下面:

*ppDoubleArray = pNewDoubleArray;
delete[] pNewDoubleArray;

删除刚传回给调用者的数组。不要删除!传递后,由调用者来管理内存。

你应该考虑编写“真正的”C ++代码,使用像std::vector这样的容器对象来管理内存。

答案 2 :(得分:1)

你的意思是这样做:

void someFunc(double** ppDoubleArray, int length)
{
     for(int i = 0; i < length; i++)
    {
        *ppDoubleArray[i] = 3 * i + 2;
    }

}

我不明白为什么你要分配一个新数组,如果你的目的是修改传入的数组。

答案 3 :(得分:0)

someFunc中分配数组,然后设置用户传递的指针,指向它。退出该函数后,您将删除该数组,并使用指向已释放内存的指针结束用户。

答案 4 :(得分:0)

您不能delete pNewDoubleArray,因为您将地址存储在ppDoubleArray中。您必须delete[] pArray,当它不再使用时或在将其设置为其他地址之前(再次呼叫someFunc(&pArray, ...)时)。