Solved

Freeing double arrays

Posted on 1998-10-12
8
210 Views
Last Modified: 2010-04-02
class CTuple
{
public:
      double *m_P;
      //long rn;
      long m_NumOfLEQ;
      double m_Att;
      double m_GD;
};

void foo(...)
{
long m_SampleSize = 20;
CTuple **m_Table;
...
double *X,A,B,GDTMP,m_Split;
...
m_Table = new CTuple* [m_SampleSize];
for (i=0; i<m_SampleSize;i++)
  {
    m_Table[i] = new CTuple [m_NumOfAtt];
    for (DWORD j = 0; j < m_NumOfAtt; j++)
    {
      m_Table[i][j].m_NumOfLEQ = 0;
      m_Table[i][j].m_P = new double [m_NumOfClass];
      for (DWORD k = 0;k < m_NumOfClass; k++)
            m_Table[i][j].m_P[k] = 0;
      }//for j
  }//for i
...
long *m_Pool = new long [m_SampleSize];
X = new double [m_SampleSize];
delete[] m_Pool;
//Cleanup
for (i=0;i<m_SampleSize;i++)
 {
      for (j=0;j<m_NumOfAtt;j++)
                  delete m_Table[i][j].m_P;
// I think the above line should delete[]
  }//for i
            
      for (i=0;i<m_SampleSize;i++)
            {
                  delete[] m_Table[i];

            }//for i cleanup
            delete[] X;
            delete[] m_Table;
}//foo
Am I deleteing all the memory I had allocated? I think I am missing something?
0
Comment
Question by:maj030598
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 1174914
Your m_P deletions are wrong, they use "delete" not "delete []"
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174915
Try

for (i=0;i<m_SampleSize;i++)
{
    for (j=0;j<m_NumOfAtt;j++)
       delete [] m_Table[i][j].m_P;   // use delete []
   delete[] m_Table[i]; // Might as well do these delete [] here instead of later.
}//for i
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174916
However, I would strongly recommend you don't use the code I posted.  you ca make this code far simpler and safer if you use constructors and destructors.  At the very least make the CTuple destructor delete the m_P array.  But you might also want to make the constructor  allocate the array as well.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:maj030598
ID: 1174917
Edited text of question
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174918
>>Am I deleteing all the memory I had allocated? I think I am missing something?
I don't see anything that is missed.  But by using constructors/destructors you can make this much easier to manage so you won't have any doubts.  Also, if you are using VC, you can use its debug heap features to detect if any memory is alocated without being freed.
0
 

Author Comment

by:maj030598
ID: 1174919
I have allocated m_P as an array
m_Table[i][j].m_P = new double [m_NumOfClass];

Y should I use delete m_Table[i][j].m_P; instead of delete[] m_Table[i][j].m_P; ?
Also how can I use the debug heap in VC 5.0?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174920
>> Y should I use delete m_Table[i][j].m_P; instead of delete[] m_Table[i][j].m_P; ?
That is backwards.  You should use "delete[] m_Table[i][j].m_P" instead of "delete m_Table[i][j].m_P"

The reason is that you allocated with "new []", not just "new". Note that

m_Table[i][j].m_P = new double [m_NumOfClass]

uses "new []", thus you must use delete [].
0
 
LVL 22

Expert Comment

by:nietod
ID: 1174921
how can I use the debug heap in VC 5.0?
There are different ways, depending on what you want to accomplist.  If you look up "debug heap" in the online help you will find lots of info.  For starters, I would use _CrtSetDbgfFlg() to set the _CRTDBG_LEAK_CHECK_DF flag.  With this flag set, when the program quits, VC  will print a list of all memory blkcjs that here allocated but not destroyed.  See the CrtSetDbfFls() function in the docs.

Another good thing to do is to overide the 4 allocation/deallocate operators (new, new [], delete, and delete[]) to have overides that take a file name and line number as parameters.    These overloaded functions should call _malloc_debug() and _free_dbg() instead of the ussual malloc() and free() that most new and delete overides call.  This allows them to specify the file name and line number when the memory is allocated.  This way, when a memory error is detected, VC will be able to report the line number where the memory was allocated. This is great help.  To make this work well use the preprocessor to define macros  ("#define")  for new and delete.  These macros then call the overloaded new and delete operators and pass the file name and line number using __FILE__ and __LINE__.  

See the docs for this.  If you have trouble let me know.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Suggested Solutions

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.
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.

828 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