内部有其他结构数组的结构的空闲内存 - C

free memory of struct that has an array of other struct inside - C

我尝试使用动态数组,当我使用完它时,我尝试释放使用的内存,但出现此错误。

free(): invalid next size (fast): 0x00652098 

这是结构变量的声明:

struct player {
int played_time;
int min_time;
int max_time;
int errors;
int color;
};

struct Players {
struct player *array;
size_t player_number;
size_t size;
};
typedef struct Players Player_list;

这是管理动态数组的方法:

void initArray(Player_list *list, size_t initialSize) {
  list->array = (struct player *) malloc(initialSize * sizeof(struct player));
  list->player_number = 0;
  list->size = initialSize;
}

void insertArray(Player_list *list, struct player element) {
  if (list->player_number == list->size) {
    list->size *= 2;
    list->array = (struct player *) realloc(list->array,
            list->size * sizeof(struct player));
  }
  list->array[list->player_number++] = element;
}

void freeArray(Player_list *list) {
  free(list->array);
  list->array = NULL;
  list->player_number = list->size = 0;
 }

int disqualified(Player_list *list, int errors) {
  int i = 0;
  for (i = 0; i < list->player_number; i++) {
    if (list->array[i].errors >= errors) {
        return 1;
    }
  }
  return 0;
  }

下面是我主要使用它的方式:

/**
 * When button is pressed 1 add an error to a random player
 */
void button_s_isr(void) {
    int index = rand() % (players.player_number);
    point_players->array[index].errors = point_players->array[index].errors      + 1;

}

      ... 

int main(void) {

      ...
// set up of GPIO
// get with scanf NUMBER_OF_PLAYERS and MAX_ERRORS values

int i;
for (i = 0; i < NUMBER_OF_PLAYERS; i++) {
    struct player player;
    player.color = PLAYER_COLORS[i];
    player.errors = 0;
    player.max_time = 0;
    player.min_time = 0;
    player.played_time = 0;
    insertArray(&players, player);
}

while (disqualified(&players, MAX_ERRORS) != 1) {
// wait
}
printf("\n  Se ha acabdo el juego: ");
freeArray(point_players);
return EXIT_SUCCESS;
}

我必须说我对 C 很陌生,如果难以理解,请原谅。 我想要做的是结构(玩家)的动态列表,其中每个玩家都有自己的参数(played_time、min_time、max_time、错误、颜色)。在主游戏中,我想要一款游戏,我可以在其中控制每个玩家的这些参数。 感谢任何改进代码的帮助。

这样的错误通常会发生,因为您正在写入超出已声明内存的末尾。虽然我们可能可以通过仔细检查每一行代码来找到错误,但 valgrind 会更快地找到它。

尝试在调试模式下编译您的代码,不进行优化(如果您使用的是 gcc,则为 gcc -g -O0)并且 运行 您的程序在 valgrind 下(例如,valgrind myprog -my-prog-options) .它应该立即标记错误。

所有这些都假设您运行正在使用 linux 的变体。如果您使用 visual studio,可能会有一个类似的内存检查器,但我不知道它是什么或如何 运行 它。 祝你好运!

发布代码:

  1. 不编译
  2. 缺少 PLAYER_COLORS[i] 的定义,这不是一个好主意,因为玩家的数量可能会超过数组中的可用颜色。
  3. 错误地计算了 realloc()
  4. 所需的大小
  5. 无法检查 malloc()realloc()
  6. 等函数的返回值
  7. 包含令人困惑的(即使对于 OP)变量和结构实例的命名
  8. 缺少 num_jugadores
  9. 的定义
  10. 错误地尝试分配结构而不是复制结构
  11. 无法声明 struct Players
  12. 的实例

现在,更正了编译干净的代码:

警告:未完全测试

#include <stdio.h>
#include <stdlib.h>
#include <string.h>  // memcpy()

struct player
{
    int played_time;
    int min_time;
    int max_time;
    int errors;
    int color;
};

struct Players
{
    struct player *array;
    size_t player_number;
    size_t numPlayers;
};


//This are the method used to manage the dynamic array:

void freeArray(struct Players *pArray)
{
    free(pArray->array);
    pArray->array = NULL;
    pArray->player_number = pArray->numPlayers = 0;
}


void initArray( struct Players *pArray )
{
    if( NULL == (pArray->array = malloc(sizeof(struct player)) ) )
    { // then malloc failed
        freeArray( pArray );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    pArray->player_number = 0;
    pArray->numPlayers = 1;
}


size_t sizeof_array(size_t size)
{
    return  size * sizeof(struct player);
}


void insertArray(struct Players *pArray, struct player *element)
{
    if (pArray->player_number == pArray->numPlayers)
    { // then currently allocated memory for array of players is full

        struct player *temp = NULL;
        if( NULL == (temp = realloc(pArray->array, sizeof_array(pArray->numPlayers)*2) ) )
        { // then, realloc failed
            freeArray( pArray );
            exit( EXIT_FAILURE );
        }

        // implied else, realloc successful

        pArray->numPlayers *= 2;
        pArray->array = temp;
    }

    memcpy( &(pArray->array[pArray->player_number]), element, sizeof( struct player ) );
    pArray->player_number++;
}

//and here is how i use it in the main method:

#define num_jugadores (20)

int main( void )
{
    int i;
    struct Players playerList;

    initArray(&playerList);

    for (i = 0; i < num_jugadores; i++)
    {
        struct player myPlayer;
        //player.color = PLAYER_COLORS[i];
        myPlayer.errors = 0;
        myPlayer.max_time = 0;
        myPlayer.min_time = 0;
        myPlayer.played_time = 0;
        insertArray(&playerList, &myPlayer);
    }


    //...

    freeArray(&playerList);
} // end function: main