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

原文 标签 c++ pointers memory-management memory-leaks

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

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?

Answer

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

翻译

以下功能中存在内存泄漏。我遇到的麻烦是知道如何,何时,何地以及要删除什么。这是代码:

#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;
}
相关推荐

c++ - 课堂上的getter和setter

c++ - malloc挂钩的目的是什么?

c++ - C++引用与传递引用参数之间的区别

c++ - “表格数据”的XML或CSV

c++ - 如何将QLabel的大小设置为其像素图中的图像大小?

c++ - MFC / WIN32 / C++:如何在执行计算密集型操作时调度消息?

c++ - std::vector查找比执行简单操作快吗?

c++ - 在glib免费错误上强制coredump

c++ - 如何将在后台运行的子进程带到前台

c++ - 在C++中使用模板时出现问题