如何对使用malloc创建的另一个结构中的结构数组使用malloc

如何对使用malloc创建的另一个结构中的结构数组使用malloc,c,malloc,C,Malloc,我对C非常陌生,只想知道在以下结构中使用malloc的正确方法: struct cell { struct player* owner; int letter; }; struct board { struct cell** matrix; int width; int height; }; 此结构位于“board”结构中,该结构也使用malloc创建。我希望这是足够的信息,我感谢任何解释。struct cell{ struct cell {

我对C非常陌生,只想知道在以下结构中使用malloc的正确方法:

struct cell {
    struct player* owner;
    int letter;
};

struct board {
    struct cell** matrix;
    int width;
    int height;
};
此结构位于“board”结构中,该结构也使用malloc创建。我希望这是足够的信息,我感谢任何解释。

struct cell{
struct cell {
    struct player* owner;
    int letter;
};

struct board {
    struct cell** matrix;
    int width;
    int height;
};

//this would work, but memory allocation is slow.
void fillBoard(board *b){
    b->matrix = (struct cell**)malloc(b->width * sizeof(struct cell*));
    for (int i = 0; i < b->height; i++){
        b->matrix[i] = (struct cell*)malloc(sizeof(struct cell));
    }
}

//to limit that you could rewrite it to this.
struct cell {
    struct player* owner;
    int letter;
};

struct board {
    struct cell* matrix;
    int width;
    int height;
};

//this only allocates memory once, so it is faster and it avoids memory fragmentation.
void fillBoard(board *b){
    b->matrix = (struct cell*)malloc(b->width * b->height * sizeof(struct cell));
}

//to access a certain cell, you have to do this (x * width + y)
struct cell *getCell(board *b, int x, int y){
    return &b->matrix[x * b->width + y];
}
结构播放器*所有者; 整数字母; }; 结构板{ 结构单元**矩阵; 整数宽度; 内部高度; }; //这会起作用,但内存分配很慢。 空心填充板(板*b){ b->matrix=(结构单元**)malloc(b->width*sizeof(结构单元*); 对于(int i=0;iheight;i++){ 矩阵[i]=(结构单元*)malloc(sizeof(结构单元)); } } //限制你可以把它改写成这样。 结构单元{ 结构播放器*所有者; 整数字母; }; 结构板{ 结构单元*矩阵; 整数宽度; 内部高度; }; //这只分配一次内存,因此速度更快,并且避免了内存碎片。 空心填充板(板*b){ b->matrix=(结构单元*)malloc(b->width*b->height*sizeof(结构单元)); } //要访问某个单元格,必须执行以下操作(x*width+y) 结构单元*getCell(板*b,整数x,整数y){ 返回(&b->矩阵[x*b->宽度+y]; }
<>编辑:我通常不在C中编程,我主要使用C++,所以可能会有一些错误。 正确使用malloc的方法

  • 使用辅助函数,而不是使单个函数过于复杂

  • 分配函数应具有匹配的自由函数

  • 检查分配和其他错误

  • 分配给引用对象的大小,而不是类型

  • 不需要强制转换
    malloc()
    的结果

例如:

#include <stdlib.h>

struct cell* cell_allocate(int width); // TBD code for OP
void cell_free(struct cell*); // TBD code for OP

void board_free(struct board *bd) {
  if (bd) {
    if (bd->matrix) {
      for (int h = 0; h < bd->height; h++) {
        cell_free(bd->matrix[h]);
      }
      free(bd->matrix);
    }
    free(bd->matrix);
  }
}

// Allocate 1 board.    
// Return NULL on error
struct board* board_allocate(int width, int height) {
  if (width <= 0 || height <= 0) {
    return NULL;
  }

  // Allocate board
  struct board *bd = malloc(sizeof *bd, 1);
  if (bd == NULL) {
    return NULL;
  }

  // Allocate matrix row pointers
  bd->matrix = malloc(sizeof *(bd->matrix) * (unsigned) height);
  if (bd->matrix == NULL) {
    board_free(bd);
    return NULL;
  }
  bd->width = width;
  bd->height = height;

  // Allocate matrix rows
  for (int h = 0; h < height; h++) {
    bd->matrix[h] = cell_allocate(width);
    if (bd->matrix[h] == NULL) {
      bd->height = h - 1;
      board_free(bd);
      return NULL;
    }
  }
  return bd;
}
#包括
结构单元*单元分配(整数宽度);//待定OP代码
空单元_free(结构单元*);//待定OP代码
无空白板(结构板*bd){
如果(bd){
如果(bd->矩阵){
对于(inth=0;hheight;h++){
无单元(bd->矩阵[h]);
}
自由(bd->矩阵);
}
自由(bd->矩阵);
}
}
//分配一块板。
//错误时返回NULL
结构板*board_分配(整型宽度、整型高度){
if(宽度矩阵)*(无符号)高度);
如果(bd->矩阵==NULL){
无电路板(bd);
返回NULL;
}
bd->宽度=宽度;
bd->高度=高度;
//分配矩阵行
对于(int h=0;h矩阵[h]=单元分配(宽度);
如果(bd->矩阵[h]==NULL){
bd->高度=h-1;
无电路板(bd);
返回NULL;
}
}
返回bd;
}

到目前为止您尝试了什么?IMO提出的作为OP结束的问题没有显示出任何努力。你为他做了家庭作业:)@0\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。