k_zaur_k k_zaur_k - 3 months ago 8
C++ Question

с++ question new delete

I have the following code

void foo()
{
char* pcBlock = new char[1000];
...
delete[] pcBlock;
...
pcBlock = new char[100000];
...
delete[] pcBlock;
}


Would the code below result in a memory leak?

void foo()
{
char* pcBlock = new char[1000];
...
pcBlock = new char[100000];
...
delete[] pcBlock;
}

Answer

Yes, there's likely a memory leak if you don't delete[] pcBlock in the first .... Reassigning a pointer does not automatically delete what it previously pointed to.

Comments