Solved

memory leak

Posted on 2003-11-15
4
418 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

688 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