C ++本机-可用内存 [英] C++ Native - free memory

查看:51
本文介绍了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:

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:

if(m_params != NULL)
    delete[] m_params;


还不够!
您还应该:


is not enough !
You should also:

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.

推荐答案

您似乎混淆了struct定义和使用它的代码-两者不使用相同的struct成员名称.

除此之外,正如CPallini所建议的那样,最简单的方法是为您的结构定义一个析构函数.并且要确保正确初始化结构中的所有指针,还应该添加一个析构函数(如果不这样做,您将不确定是否非NULL指针实际上指向已分配的内存,或者尚未初始化).

尝试以下操作:
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:
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;


请注意,当您调用newdelete时,构造函数和析构函数将被自动调用,因此您不必在释放内存的任何地方进行清理.


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.


您的第一个问题是它将无法编译,因为您正在代码中使用结构中不存在的变量名.在所有情况下,如果结构中有指向已分配内存块的元素,则应在删除结构之前删除它们(检查!= NULL而不是==).
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 ==).


我认为您的struct初始化与它的定义不匹配.
我还认为您不需要在C++中的typedef一个struct,并且可以使用struct destructor分配的删除内存(如果已分配)来初始化struct的成员.
另外,如果使用std::vector而不是普通数组,则不必担心数组分配(和取消分配).
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).


这篇关于C ++本机-可用内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆