C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 无法释放内存大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法真正理解为什么自由进程会返回错误.我在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) ...*/

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

大佬总结

以上是大佬教程为你收集整理的c – 无法释放内存全部内容,希望文章能够帮你解决c – 无法释放内存所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。