I have an array of lever 2 pointer **f[5] -> f[5][6][7], I want to allocate them on Heap memory and then free them, how to do this? Thanks.
I get memory leak when I do it:
- Allocate them:
for (int i=0;i<5;i++)
{
f[i]= new int*[6];
for (int j=0;j<6;j++)
f[i][j]= new int[7];
}
And free them as follows:
for (i=0;i<6;i++)
delete[] f[i];
My full code is as follows:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int i, j;
int **f[5];
for (i=0;i<5;i++)
{
f[i]= new int*[6];
for (j=0;j<6;j++)
f[i][j]= new int[7];
}
for (i=0;i<5;i++)
for (j=0;j<6;j++)
for (int k=0;k<7;k++)
f[i][j][k]=i*j*k;
for (i=0;i<5;i++)
{
for (j=0;j<6;j++)
delete[] f[i][j];
delete[] f[i];
}
return 0;
}
And I get two warnings (the name of my project is "a"):
Warning 1 warning C6211: Leaking memory 'f[0]' due to an exception. Consider using a local catch block to clean up memory: Lines: 9, 10, 12, 13, 14 c:\users\peter\desktop\a\a\a.cpp 12 a
Warning 2 warning C6211: Leaking memory 'f[0][8]' due to an exception. Consider using a local catch block to clean up memory: Lines: 9, 10, 12, 13, 14, 13, 14, 13, 14, 13, 10, 12 c:\users\peter\desktop\a\a\a.cpp 14 a
I dont understand them. Can you help me check it. Thanks.