Solved

Freeing double arrays

Posted on 1998-10-12
8
204 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
 

Author Comment

by:maj030598
ID: 1174917
Edited text of question
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 clear a vector as well as how to detect empty vectors in C++.

863 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