Solved

Freeing double arrays

Posted on 1998-10-12
8
201 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
Comment Utility
Your m_P deletions are wrong, they use "delete" not "delete []"
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
Edited text of question
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

16 Experts available now in Live!

Get 1:1 Help Now