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开发者_JAVA百科 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?
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;
}
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.
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.
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.
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).
精彩评论