加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 无法释放内存

发布时间:2020-12-16 10:16:19 所属栏目:百科 来源:网络整理
导读:我无法真正理解为什么自由进程会返回错误.我在C中得到了这段代码: int LuffarschackStart(void){/* to avoid the program from closing */char readEnd;int i = 0; board_type *board = malloc(sizeof(square_type));if (board == NULL){ printf("Could not
我无法真正理解为什么自由进程会返回错误.我在C中得到了这段代码:

int LuffarschackStart(void)
{
/* to avoid the program from closing */
char readEnd;
int i = 0;    

board_type *board = malloc(sizeof(square_type));
if (board == NULL)
{
    printf("Could not allocate the memory needed...");
    scanf("%c",&readEnd);         
    return 0;
}

for(i = 0; i < 9; i = i + 1)
    board->square[i].piece_type = NO_PIECE;

board_play_game(board);    

free(board);
printf("Press any key and enter to quit the program...");
scanf("%c",&readEnd);         
return 0;
}

我分配的板结构如下所示:

typedef struct
{
    /* flag to indicate if a square is free or not */  
    int free;
    /* the type of piece stored on the square if the 
       square is not free,in this case the admissible 
       values are CROSS_PIECE and CIRCLE_PIECE,otherwise the value NO_PIECE is used */ 
    int piece_type; 
} square_type; 

typedef struct
{
    square_type square[N_SQUARES]; 
    int computer_type;
    int player_type;
} board_type;

可能问题是我需要首先释放板内的square_type吗?如果是这样的话,我如何解放?

解决方法

我认为你的malloc是错误的.它应该是

board_type *board = malloc(sizeof(board_type)); /* instead of sizeof(square_type) ...*/

除此之外,我认为你的代码是正确的……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读