Solved

memory leak

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now