Click here to Skip to main content
11,415,973 members (84,210 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
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:

// 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"):
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 8-Feb-13 14:59pm
Edited 9-Feb-13 18:03pm
v6
Comments
Sergey Alexandrovich Kryukov at 8-Feb-13 20:09pm
   
What did you try so far? It seems to be a very basic thing.
—SA
Andrewpeter at 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 at 8-Feb-13 22:50pm
   
Sure. Next time, always write code in the body of your question. You could use "Improve question".
—SA
Andrewpeter at 9-Feb-13 7:30am
   
Thanks SA, I improved my question.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Andrewpeter at 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 at 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 at 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 at 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 at 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
Andrewpeter at 12-Feb-13 7:14am
   
I'm sorry, I don't understand about what you say. You can give me an example? Thanks.
Sergey Alexandrovich Kryukov at 12-Feb-13 11:15am
   
What example?! Get rid of immediate constants like 5, 6 and 7. Explicitly declare them outside of your code. If you hard-code, you will never get valid results. Should you ever change 7 to 9, what are you going to do. As soon as you are hard-coding, you are not programming, just messing around.
—SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 9,270
1 OriginalGriff 7,512
2 Maciej Los 3,710
3 Abhinav S 3,325
4 Peter Leow 3,084


Advertise | Privacy | Mobile
Web01 | 2.8.150427.4 | Last Updated 9 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100