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;
删除刚刚传递回调用方的数组。不要这样做删除!在将内存传回后,由调用方来管理内存。
与其跳过所有这些箍,不如考虑编写"真正的"C++代码,使用像std::vector
这样的容器对象来管理内存。
你的意思是这样做吗:
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, ...)
时)。
相关文章:
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 如果我真的真的想从 STL 容器继承,并且我继承构造函数并删除新运算符,会发生什么?
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 我什么时候会默认(而不是删除)基类中的复制和移动操作
- 删除对象(具有不同类型)的引用时会发生什么情况?
- 在析构函数中删除单链表的正确方法是什么?
- 处理从列表中删除指向对象的指针的"healthy"方法是什么?
- 将此私有删除器函数包装在结构中的目的是什么?
- 删除类成员的动态分配内存的最佳方法是什么
- 除了调用全局删除运算符之外,删除一个void指针还能做什么呢
- 在这种情况下,删除指针数组期间会发生什么?
- 为什么你会=删除隐式删除的默认构造函数,有什么意义?
- 当删除或删除[]被执行时,计算机会做什么?
- 如果在删除后使用 delete[] 会发生什么?
- 擦除remove_if成语 - 删除了什么吗?
- 如果我不删除动态创建的变量会发生什么
- 删除数组成员的更好方法是什么?
- 删除默认类构造函数有什么意义?
- 如果我在c++中删除一个指针,会删除什么
- C++内存泄漏 - 我删除什么,在哪里删除