Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Freeing double arrays

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
maj030598
Asked:
maj030598
  • 6
  • 2
1 Solution
 
nietodCommented:
Your m_P deletions are wrong, they use "delete" not "delete []"
0
 
nietodCommented:
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
 
nietodCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
maj030598Author Commented:
Edited text of question
0
 
nietodCommented:
>>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
 
maj030598Author Commented:
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
 
nietodCommented:
>> 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
 
nietodCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now