C++内存泄漏 - 我应该删除什么,在哪里删除?
以下函数中存在内存泄漏。我遇到的麻烦是不知道如何、何时、何地以及删除什么。这是代码:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您正在分配 &删除函数中的数组。而你也正在归还它。
// 这个是在堆栈上分配的,所以退出 main() 时它将被删除
// 你的函数现在分配了 pArray 指向的一些内存
// 你忘记删除你的函数分配的内存了!内存泄漏!!!
You are allocating & deleting an array in the function. And you are also returning it.
// This one is allocated on the stack, so it will be deleted when exiting main()
// Your function allocates some memory now pointed at by pArray
// Your forgot to delete the memory allocated by your function!!! Memory leak!!!
这里:
您删除刚刚传递回调用者的数组。不要这样做删除!传回内存后,由调用者来管理内存。
您应该考虑编写“真正的”C++ 代码,使用像
std::vector
这样的容器对象来为您管理内存,而不是跳过所有这些环节。Here:
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.您是否打算这样做:
如果您的目的是修改传入的数组,我不明白为什么要分配一个新数组。
Did you mean to do this:
I don't understand why you'd allocate a new array if your intent is to modify the one passed in.
在
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.您无法
删除
pNewDoubleArray
,因为您将其地址存储在ppDoubleArray
中。当不再使用或将其设置为另一个地址之前(再次调用someFunc(&pArray, ...)
时),您必须删除[] pArray
。You cannot
delete
pNewDoubleArray
, as you store its address inppDoubleArray
. You have todelete[] pArray
, when it is not used anymore or before setting it to another address (when callingsomeFunc(&pArray, ...)
again).