Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Freeing double arrays

Posted on 1998-10-12
8
Medium Priority
?
213 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
[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
  • 6
  • 2
8 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

721 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