如何用'new'和'delete'替换'malloc'和'free'?

Ras*_*oul 1 c++ malloc new-operator

如何更改以下代码,以便我可以使用newdelete不是mallocfree

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;

class myclass{
  private:
    float **m_R;
  public:
    myclass();
    ~myclass();
    float **getR(void);
    void setR(void);
};

myclass::myclass()
{
  // first allocate rows (for pointers)
  m_R = (float**)malloc(3*sizeof(float));
  // next allocate columns for float values
  for(int i=0;i<3;i++)
    *(m_R+i) = (float*)malloc(3*sizeof(float));
}

myclass::~myclass()
{
  // first free memory allocated by columns
  for(int i = 0; i < 3; i++)
  {
    free(m_R[i]);
  }
  // next free memory allocated by rows
  free(m_R);
}

void myclass::setR(void)
{
  for(int i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      m_R[i][j] = 10*i+j;
      //cout << m_R[i][j] << ", ";
    }
    //cout << "\n";
  }
}

float **myclass::getR(void)
{
  return m_R;
}

int main () {

  myclass obj;
  obj.setR();

  for(int i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      printf("%02d, ",(int)obj.getR()[i][j]);
    }
    cout << "\n";
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑1:请注意我必须使用一个函数(不是我写的)float**作为参数,我没有选择(比如vector)而不是使用float**.

Edit2:该函数来自Proximity Query Package(PQP),其编写如下:

int 
PQP_Distance(PQP_DistanceResult *result, 
             PQP_REAL R1[3][3], PQP_REAL T1[3], PQP_Model *o1,
             PQP_REAL R2[3][3], PQP_REAL T2[3], PQP_Model *o2,
             PQP_REAL rel_err, PQP_REAL abs_err,
             int qsize = 2);
Run Code Online (Sandbox Code Playgroud)

Mik*_*one 9

  • T* a = (T*)malloc(sizeof(T))成为new T.
  • T* b = (T*)malloc(N * sizeof(T))成为new T[N].
  • free(a)成为delete a.
  • free(b)成为delete[] b.

所以你得到:

myclass::myclass()
{
  // first allocate rows (for pointers)
  m_R = new float*[3];
  // next allocate columns for float values
  for(int i=0;i<3;i++)
    *(m_R+i) = new float[3];
}

myclass::~myclass()
{
  // first free memory allocated by columns
  for(int i = 0; i < 3; i++)
  {
    delete[] m_R[i];
  }
  // next free memory allocated by rows
  delete [] m_R;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这实际上不是最佳选择.如果你想要3x3矩阵,你最好在一个块中分配9个浮点数.

另请注意您的C不正确.

m_R = (float**)malloc(3*sizeof(float));
Run Code Online (Sandbox Code Playgroud)

应该

m_R = (float**)malloc(3*sizeof(float*));
Run Code Online (Sandbox Code Playgroud)

您的代码可能"有效",因为您正在编译32位,其中floatfloat*4个字节.在64位版本上,float*是8个字节.


但老实说,既然你的尺寸固定而且很小,你应该将所有东西存储在对象本身中:

class myclass{
  private:
    float m_R[3][3];
  public:
    myclass() {}
    ~myclass() {}
    void setR(void);
    float* operator[](unsigned i) { return m_R[i]; }
    const float* operator[](unsigned i) const { return m_R[i]; }
};

void myclass::setR(void)
{
  for(int i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      (*this)[i][j] = 10*i+j;
      //cout << (*this)[i][j] << ", ";
    }
    //cout << "\n";
  }
}

int main () {

  myclass obj;
  obj.setR();

  for(int i=0;i<3;i++)
  {
    for(int j=0;j<3;j++)
    {
      printf("%02d, ",(int)(obj[i][j]));
    }
    cout << "\n";
  }

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这就是我通常这样做的方式,所以我得到了[] []的可读性和对象存储的效率.


das*_*ght 6

假设使用标准C++库是一个选项,您应该使用

std::vector<std::vector<float> > m_R;
Run Code Online (Sandbox Code Playgroud)

而不是float**.绝对没有缺点,你可以免费获得许多方便的东西.例如,您可以找到向量的大小及其每个维度,而不会在侧面传递一对数字,或者在某些假设中进行编码.您可以在没有循环的情况下进行分配,并且根本不删除任何代码.

如果这不是一个选项,您可以替换malloc/ freenew[]/ delete[]如下:

// Creating
float **m_R = new float*[10];
for (int i = 0 ; i != 10 ; i++) {
    m_R[i] = new float[20];
}

// Deleting
for (int i = 0 ; i != 10 ; i++) {
    delete[] m_R[i];
}
delete[] m_R;
Run Code Online (Sandbox Code Playgroud)