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

时间:2016-04-16 13:22:35

标签: c pointers struct free dynamic-memory-allocation

嘿,我通过释放结构上的元素来解决问题

长码警告

    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 collumns
            {

            //
                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()函数的无知,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

伊兰,

不要害怕使用必要的包含发布您的代码。首先,你是否听从了编译器的警告?

一些问题领域:

while (temp[k] == NULL)

您可以研究0,NULL和'\ 0'之间的区别,但保留对指针使用NULL。也:

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

Printf期待一个整数,你给它一个指针。最后,为了解决我认为你的问题,当你写“...中断free()函数来释放内存?”你是说你的程序根本不回来?如果是这样,那么摆脱最后的getchar()。你仍然会在这个程序中至少有一个泄漏。这个malloc的地址:

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

将丢失,因为您分配了运气。在此处添加新地址而不释放第一个:

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