我想知道这两个功能有什么区别。
在 C++ 中,new 和 malloc 之间的区别更简单:new 通过调用构造函数来分配内存。
PETSc 怎么样?在示例中,它似乎非常接近:
在 dm/examples/tutorials/ex6.c 中:
struct _p_FA {
MPI_Comm comm[3];
PetscInt xl[3],yl[3],ml[3],nl[3]; /* corners and sizes of local vector in DMDA */
PetscInt xg[3],yg[3],mg[3],ng[3]; /* corners and sizes of global vector in DMDA */
PetscInt offl[3],offg[3]; /* offset in local and global vector of region 1, 2 and 3 portions */
Vec g,l;
VecScatter vscat;
PetscInt p1,p2,r1,r2,r1g,r2g,sw;
};
typedef struct _p_FA *FA;
FA fa;
PetscNew(&fa);
在 ksp/ksp/examples/tutorials/ex42.c 中:
PetscMalloc(sizeof(struct _p_CellProperties),&cells);
规则就是:将 PetscMalloc 用于结构,将 PetscNew 用于结构指针?