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

发布于 2024-11-06 21:38:26 字数 2089 浏览 1 评论 0原文

以下函数中存在内存泄漏。我遇到的麻烦是不知道如何、何时、何地以及删除什么。这是代码:

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

这是否解决了这种情况下的任何内存泄漏(如果有的话)?

A memory leak exists in the following function. The trouble I am having is knowing how, when, where, and what to delete. Here is the code:

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

As you can see, I tried what to delete the new array I allocated after I had used it. It actually makes sense that this didn't work, but I am not sure what to do here..

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

Does this solve any, if at all memory leaks in this situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

折戟 2024-11-13 21:38:26

您正在分配 &删除函数中的数组。而你也正在归还它。

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

You are allocating & deleting an array in the function. And you are also returning it.

int main()
{

// This one is allocated on the stack, so it will be deleted when exiting main()

    double dbls[] = { 1, 2, 3, 4, 5 };

    double* pArray = dbls;

    //...

// Your function allocates some memory now pointed at by 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){ }

// Your forgot to delete the memory allocated by your function!!! Memory leak!!!

    return 0;
}
唯憾梦倾城 2024-11-13 21:38:26

这里:

*ppDoubleArray = pNewDoubleArray;
delete[] pNewDoubleArray;

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

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

Here:

*ppDoubleArray = pNewDoubleArray;
delete[] pNewDoubleArray;

You delete the array that you just passed back to the caller. Don't do that delete! It is up to the caller to manage the memory after you pass it back.

Rather than jump through all these hoops, you should consider writing "real" C++ code, using container objects like std::vector which will manage the memory for you.

伏妖词 2024-11-13 21:38:26

您是否打算这样做:

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

}

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

Did you mean to do this:

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

}

I don't understand why you'd allocate a new array if your intent is to modify the one passed in.

触ぅ动初心 2024-11-13 21:38:26

someFunc 中,您分配数组,然后设置用户传递的指针以指向它。退出该函数后,您将删除该数组,并且用户最终会得到一个指向已释放内存的指针。

In someFunc you allocate array, then set pointer passed by the user, to point to it. Upon exiting the function, you delete that array and user ends-up with a pointer to freed memory.

相守太难 2024-11-13 21:38:26

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

You cannot delete pNewDoubleArray, as you store its address in ppDoubleArray. You have to delete[] pArray, when it is not used anymore or before setting it to another address (when calling someFunc(&pArray, ...) again).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文