结构的空闲内存,里面有一个其他结构数组--C

时间:2016-05-16 13:42:47

标签: c arrays memory-management struct free

我尝试使用动态数组,当我完成使用它时,我尝试释放使用的内存,我收到此错误。

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

这是struct变量的声明:

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很新,对不起,如果难以理解的话。 我想要做的是结构(玩家)的动态列表,其中每个玩家都有自己的参数(playing_time,min_time,max_time,错误,颜色)。在主要内部我希望有一个游戏,我可以从每个玩家控制这些参数。 任何改进代码的帮助都表示赞赏。

2 个答案:

答案 0 :(得分:0)

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

尝试在没有优化的情况下在调试模式下编译代码(如果使用gcc,请使用gcc -g -O0)并在valgrind下运行程序(例如,valgrind myprog -my-prog-options)。它应该立即标记错误。

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

答案 1 :(得分:0)

发布的代码:

  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