Click here to Skip to main content
15,904,023 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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:
C++
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:
C++
for (i=0;i<6;i++)
     delete[] f[i];


My full code is as follows:

C#
// a.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    int i, j;
    int **f[5];   // f[5][6][7]
    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"):
C++
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.
Posted
Updated 9-Feb-13 17:03pm
v6
Comments
Sergey Alexandrovich Kryukov 8-Feb-13 20:09pm    
What did you try so far? It seems to be a very basic thing.
—SA
Andrewpeter 8-Feb-13 20:14pm    
Yes Sir, but 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];

Please help me solve this, thanks.
Sergey Alexandrovich Kryukov 8-Feb-13 22:50pm    
Sure. Next time, always write code in the body of your question. You could use "Improve question".
—SA
Andrewpeter 9-Feb-13 7:30am    
Thanks SA, I improved my question.

1 solution

First of all, you are using immediate constants 5, 6 and 7. Even if you had no bugs, it's absolutely unacceptable, as it makes your code totally unsupportable. You need only two constants: the inner array dimension, and the dimension of the array of arrays; and you have to declare them explicitly.

Now, without showing a solution (please do it by yourself; only in this case you can learn to do something), I'll show you the apparent bug:

You do allocation in two nested loops. In outer loop, you call the operator new 5 times, in inner loop, you do it 6 times in each iteration, so you call call the operator new (6 + 1) * 5 = 35 times altogether.
When you do deallocation, you call delete[] in a single loop, 6 times. No wonder you have a memory leak.

Now, think just a bit and fix it. If it's hard to imagine, draw the allocated memory area on paper and write the code. This is really easy.

Good luck,
—SA
 
Share this answer
 
Comments
Andrewpeter 9-Feb-13 7:33am    
I have two problems:
- I allocate it truly or wrong?
- I free it wrong, how to do it truly? I want to understand it well. Please explain it clearly. Thanks.

My vote is 5 for your answer, it's great with me.
Andrewpeter 9-Feb-13 7:40am    
My code was fixed as follows:
- Allocate:
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];
}
- Free:
for (int i=0;i<5;i++)
{
delete[] f[i];
for (int j=0;j<6;j++)
delete[] f[i][j];
}

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.
Sergey Alexandrovich Kryukov 9-Feb-13 12:25pm    
We can discuss it further, but only if you comb it:
1) move it to the question, where it will be formatted (use "Improve question") with "pre" tags;
2) get rid of 5, 6 and 7, at least declare then explicitly as constants; better yet, make two functions, pass array and dimensions as parameters; and write the code calling them.
3) include declaration of f.
—SA
Andrewpeter 9-Feb-13 23:08pm    
I updated my post, hope you help me. I biuld well, but when I analyze with "Run code analysis on "a"" and I get the warnings above.
Sergey Alexandrovich Kryukov 9-Feb-13 23:15pm    
Can you finally get rid of your hard-coded immediate constants 5, 6, 7? First, you should never ever use then, and it hurts to look at them.
Actually, it's more important than all you bugs with you will eventually fix, of course...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900