Click here to Skip to main content
15,918,275 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
typedef struct param_struct
{
    float   A1;    
    float   A2;        
    float   A3;              
    float   *A4;       
    float   *A5;        
    float   *A6;    
    float   *A7;    
    int     A8; 
    float   *A9, *A10; 
 } PARAMS_STRUCT;

Is this correct:

C++
PARAMS_STRUCT* m_params;

if(m_params != NULL)
    delete[] m_params; 
m_params = NULL;
m_params = new PARAMS_STRUCT[m_NumOfClasses*(m_NumOfClasses-1)/2];
    for (int i = 0; i < m_NumOfClasses*(m_NumOfClasses-1)/2; i++)
    {
        m_params[i].A4 = NULL;
        m_params[i].A5 = NULL;
        m_params[i].A6 = NULL;
        m_params[i].A7 = NULL;
        m_params[i].A9 = NULL;
        m_params[i].A10 = NULL;
    }


I think that:
C++
if(m_params != NULL)
    delete[] m_params;

is not enough !
You should also:
C++
for (int i = 0; i < m_NumOfClasses*(m_NumOfClasses-1)/2; i++)
   {
       if ( m_params[i].A4 == NULL)
       {
         delete m_params[i].A4;
         m_params[i].A4 = NULL;
       }
//      and so
       m_params[i].A5 = NULL;
       m_params[i].A6= NULL;
       m_params[i].A7 = NULL;
       m_params[i].A9 = NULL;
       m_params[i].A10 = NULL;
   }

Am I right? 
Thanks,

Tal.
Posted
Updated 6-Mar-12 23:38pm
v7

You seem to have mixed up your struct definition and the code that uses it - the two don't use the same struct member names.

Apart from that, as CPallini suggested, the easiest way is to define a destructor to your struct. And to make sure that all pointers within the struct are initialized correctly, you should also add a destructor (if you don't, you won't know for sure whether a pointer that is not NULL actually points to allocated memory, or just hasn't been initialized).

Try this:
C++
struct PARAMS_STRUCT {
   /**
     * default constructor
     */
   PARAMS_STRUCT() 
      : mean_x(NULL),      // here starts the initializer list
        std_x(NULL),
        alfaXlabels(NULL),
        train_mat(NULL),
        pDst(NULL),
        dot_b(NULL)
   {
      // more initialization, if you need it
   }

   /**
    * destructor
    */
   ~PARAMS_STRUCT()
   {
      delete mean_x;
      delete std_x;
      delete alfaXlabels;
      delete train_mat;
      delete pDst;
      delete dot_b;
   }

   float* mean_x;
   float* std_x;
   float* alfaXlabels;
   float* train_mat;
   float* pDst;
   float* dot_b;
};


// and later:

   PARAMS_STRUCT* m_params = new PARAMS_STRUCT[m_NumOfClasses*(m_NumOfClasses-1)/2];

// and when you're finished:

   delete [] m_params;

Note that the constructor and destructor will be called automatically when you invoke new and delete, so you don't have to take care of cleaning up everywhere you release your memory.
 
Share this answer
 
Your first problem is that this will not compile, you are using variable names in your code that do not exist in your structure. In all cases if you have elements in your structure that point to allocated memory blocks then you should delete them before deleting the structure (check != NULL rather than ==).
 
Share this answer
 
I think that your struct initialization doesn't match its definition.
I also think you don't need to typedef a struct in C++ and that you may use struct destructor the delete memory allocated (if allocated) for initializing struct's members.
Also, if you use at std::vector instead of a plain array, then you don't have to worry about array allocation (and deallocation).
 
Share this answer
 
Comments
Stefan_Lang 7-Mar-12 4:53am    
I agree on the typedef part but it made me wonder: I do remember this style of declaration to be common practice some 20 years ago, but don't quite recall why it was ever introduced, and what has changed in the meantime that now people consider it obsolete.
CPallini 7-Mar-12 5:22am    
In C language you still need it.
Stefan_Lang 7-Mar-12 6:04am    
Ok, that still doesn't explain what part is needed. But nevermind, I looked it up.

For those wondering as well: It seems that in C the definition struct foo{}; defines the type name as 'struct foo', rather than just 'foo', so the typedef was just a means to shorten the type name.
CPallini 7-Mar-12 6:22am    
Exactly. The same applies, for instance, to union.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900