繁体   English   中英

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

[英]C++ Memory Leak - What do I delete, and where?

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

#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;
}

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

您正在分配和删除函数中的数组。 你也回来了。

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;
}

这里:

*ppDoubleArray = pNewDoubleArray;
delete[] pNewDoubleArray;

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

你应该考虑使用像std::vector这样的容器对象来编写“真正的”C ++代码,而不是跳过所有这些箍,它将为你管理内存。

你的意思是这样做的:

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

}

如果您的意图是修改传入的数组,我不明白为什么要分配新数组。

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

您无法delete pNewDoubleArray ,因为您将其地址存储在ppDoubleArray 你必须delete[] pArray ,当它不再使用时或在将它设置到另一个地址someFunc(&pArray, ...)再次调用someFunc(&pArray, ...)时)。

暂无
暂无

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

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