C:在结构元素上使用Free()的问题,奇怪的行为

dtcbnfnu  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(118)

释放结构体上的元素时遇到问题。

  • 长代码警告 *
typedef struct bingo
{
    char board[5][5];
    int* luckNum;
    int* boardNum;
} bingo;

void update(bingo *pBingo,int num); //Function that gets a struct, number and checks if he is in the board, if it does he change it to "X"

int main(void)
{
    srand(time(NULL));
    int i, j, m, k, temp[75], *parr;
    bingo player;

    //For rellocating them later
    if (!(player.luckNum = (int*) malloc(sizeof(int))))
    {
        printf("ERROR");
    }

    if (!(player.boardNum = (int*) malloc(sizeof(int))))
    {
        printf("ERROR");
    }
    //giving temp values of 1-75
    for ( i = 0; i < 75; i++)
    {
        temp[i] = i + 1;
    }


    //Giving the player board random values of 1-75 without repeating the same number twice 
        for ( i = 0; i < 5; i++) //Passing on the rows
        {
            for (j = 0; j < 5; j++) //Passing on the columns
            {

            //
                do
                {
                    k = rand() % 75; //from 0-74 
                }
                while (temp[k] == NULL); //while temp[k] is marked 
                player.board[i][j] = temp[k];
                temp[k] = NULL; //NULL as a "flag" that marks the cell as taken (for not taking the same number twice)
                player.luckNum=(int*) malloc(sizeof(int)*(i*j+j));
                player.luckNum[i*j + j] = player.board[i][j];
            }
        }

    //sets luckNum
        for ( i = 0; i < 25; i++)
    {
        printf("%d ", player.luckNum[i]);
        update(&player, player.luckNum[i]);
    }

    printf("\n");
    for ( i = 0; i < 25; i++)
    {
        printf("%d",player.luckNum);
    }

    free(player.boardNum);
    free(player.luckNum);


    

    getchar();  
    return 0;
}

void update(bingo *pBingo, int num)
{
    int i, j, k;
    static int counter = 0,luckCounter = 25;


    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 5; j++)
        {
            if (num == (int) (pBingo->board[i][j]))
            {
                pBingo->board[i][j] = 'X';
                counter++;
                pBingo->boardNum = (int*) realloc(pBingo->boardNum, sizeof(int)*counter);
                pBingo->boardNum[counter] = num;

                for (k = 0; k < luckCounter; k++)
                {
                    if (pBingo->luckNum[k] == num)
                    {
                        num = pBingo->luckNum[k];
                        pBingo->luckNum[k] = pBingo->luckNum[luckCounter-1];
                        pBingo->luckNum[luckCounter-1] = num;
                        pBingo->luckNum = (int*) realloc(pBingo->luckNum, sizeof(int)*luckCounter);
                        luckCounter--;
                    }
                }
            }
    
        }
    }

}

有人知道是什么中断了free()函数释放内存吗?.我是C新手,正在写这段代码,很抱歉我对free()函数一无所知,有人能帮我吗?

o2rvlv0m

o2rvlv0m1#

宜兰
不要害怕发布你的代码和必要的include。首先,你注意到编译器的警告了吗?
一些问题领域:
while (temp[k] == NULL)
您可以研究0、NULL和“\0”之间的差异,但要保留NULL用于指针。另外:

for ( i = 0; i < 25; i++)
   {
       printf("%d",player.luckNum);
   }

Printf需要一个整数,而你给了它一个指针。最后,为了解决我认为是你的问题,当你写“.. interrupt the free()function from free the memory?”时,你的意思是你的程序只是不返回吗?如果是这样,那么去掉最后的getchar()。你仍然会在这个程序中有至少一个漏洞。这个malloc的地址是:

if (!(player.luckNum = (int*) malloc(sizeof(int))))

将在您分配幸运值时丢失。请在此输入新地址,但不释放第一个地址:

player.luckNum=(int*) malloc(sizeof(int)*(i*j+j));

相关问题