Solved

memory leak

Posted on 2003-11-15
4
417 Views
Last Modified: 2010-04-02
To C++ experts,

 suppose I have this code :
--------------------------------
  int **p = new int*[dim1] ;
  for(int i=0; i<dim1; i++)  p[i] = new int[dim2] ;

  for(int i=0; i<dim1; i++)
    for(int j =0; j< dim2; j++) p[i][j] = i*j ;
--------------------------------
    Then how do I  free the memory ?
    Also, in general, is there a way to check the memory leak or keep  
    track of the memory usage ?

    Thanks.

 meow..
0
Comment
Question by:meow00
  • 4
4 Comments
 
LVL 19

Accepted Solution

by:
mrwad99 earned 60 total points
ID: 9754601
// Delete the memory allocated to each of the sub-arrays
for (i = 0; i < dim2; i++) {
     delete[] p[i];
}

// Then delete the memory for the main array p
delete[] p;
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 9754609
>Also, in general, is there a way to check the memory leak or keep  track of the memory usage ?

If you are using VC++ then there are some tools out there that claim to be able to do just this, but whether they work or not is a different matter.  I have never used any.

BTW, linking in with your next question, it should be clear now that this memory is allocated on the HEAP.

:)
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 9754624
PS it is not advisable to post the same question in a separate topic, you should post a 20 point Q with a link to this one, or request a zero point pointer question from community support.  That way you can delete it yourself.

:D
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 9765880
Thanks a lot for the points !

:)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question