Solved

memory leak

Posted on 2003-11-15
4
415 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ question 3 65
Error C2678: binary '!=': no operator found... 4 49
Why isn't object file created? 6 59
draw a Christmas tree by using a nested loop? 26 62
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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