Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

memory leak

Posted on 2003-11-15
4
Medium Priority
?
421 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 240 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
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.
Suggested Courses

597 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