簡體   English   中英

評論 2 免費時出現分段錯誤(推箱子游戲)

[英]Segmentation fault when commenting 2 free (Sokoban game)

從昨天開始我一直在嘗試解決分段錯誤,這讓我發瘋了......所以我有這個程序來實現推箱子游戲,其中給出了地圖,以及像“NSEW”這樣的計划是給定的,這意味着我希望玩家在給定的地圖上向北移動,然后向南移動,然后等等。

map* replay(map* map_loaded, int length, char* plan){
    map* new_map=move(map_loaded,plan[0]);
    map* old_map=deep_copy(new_map);
    for (int i=1 ; i<length ; i++){
        free(new_map->p_char);
        free(new_map);
        new_map=move(old_map,plan[i]);
        free(old_map->p_char);
        free(old_map);
        old_map=deep_copy(new_map);
    }
    free(old_map->p_char);
    free(old_map);
    return new_map;
}

Map 是一個定義如下的結構:

typedef struct map map;
struct map{
    int width;
    int height;
    char* p_char;
};

move 函數做一個動作;

deep_copy 制作地圖結構的深層副本:

map* deep_copy(map* map_loaded){

    map* new_map=malloc(sizeof(map));

    char* p_array=map_loaded->p_char;
    int width=map_loaded->width;
    int height=map_loaded->height;
    new_map->width=width;
    new_map->height=height;

    char* p_new=malloc(sizeof(char)*width*height);
    for (int i=0 ; i<width*height ; i++){
        p_new[i]=p_array[i];
    }
    new_map->p_char=p_new;
    return(new_map);
}

會發生的情況是,如果我保留這樣的代碼,如果選擇的長度(我希望玩家做的動作次數)太高(取決於地圖和動作),則在執行 replay.c 程序時:

int main(int argc, char *argv[]){
    if (argc != 4) {
        fprintf(stderr, "You must provide a file name or enough elements!\n");
        exit(EXIT_FAILURE);
    }

    map* map_loaded=load(argv[1]);

    int length=atoi(argv[2]);

    char* plan=argv[3];


    map* new_map=replay(map_loaded,length,plan);
    print_map(new_map);

    free(new_map->p_char);
    free(new_map);
    free(map_loaded->p_char);
    free(map_loaded);
    return 1;
}

我遇到了分段錯誤...但如果我這樣做:

//free(old_map->p_char);
//free(old_map);

一切正常! 我真的不明白為什么......問題是如果我做那些免費的valgrind告訴我我沒有alloc那么多的免費,這是正常的......

我真的很感激所提供的任何幫助......如果你有足夠的勇氣閱讀我直到這一點,在此先感謝!

編輯:這是我的移動功能;

   map* move(map* map_loaded, char dir){
    int pos=Position(map_loaded);
    int width=map_loaded->width;

    map* new_map=deep_copy(map_loaded);
    char* p_new_array=new_map->p_char;


    switch(dir){
        case 'N':
        if (p_new_array[pos-width]=='#'){ // Si il y a un mur au dessus
                return(map_loaded);
            }
            if ((p_new_array[pos-width]=='$' && p_new_array[pos-2*width]=='$')
              ||(p_new_array[pos-width]=='$' && p_new_array[pos-2*width]=='*') 
              ||(p_new_array[pos-width]=='*' && p_new_array[pos-2*width]=='$') 
              ||(p_new_array[pos-width]=='*' && p_new_array[pos-2*width]=='*')){ //s'il y a 2 caisses au dessus
                return(map_loaded);
            }
            if ((p_new_array[pos-width]=='$' && p_new_array[pos-2*width]=='#') //s'il y a une caisse au niveau -1 et un mur au niveau -2
              ||(p_new_array[pos-width]=='*' && p_new_array[pos-2*width]=='#')){
                return(map_loaded);
            }

            // On vérifie d'abord s'il y a une caisse à déplacer

            if (p_new_array[pos-width]=='$' || p_new_array[pos-width]=='*'){
                if (p_new_array[pos-2*width]=='.'){
                    p_new_array[pos-2*width]='*';
                }   
                else {
                    p_new_array[pos-2*width]='$';
                }

                if (p_new_array[pos-width]=='*'){
                    p_new_array[pos-width]='.';
                }
                else{
                    p_new_array[pos-width]=' ';
                }   
            }


            //On change le char en position du joueur 

            if (p_new_array[pos]=='+'){
                p_new_array[pos-width]='@';
                p_new_array[pos]='.';

            }
            else if (p_new_array[pos-width]=='.'){
                p_new_array[pos-width]='+';
                p_new_array[pos]=' ';
            }
            else {
                p_new_array[pos-width]='@';
                p_new_array[pos]=' ';
            } 
            break;



        case 'S':
        if (p_new_array[pos+width]=='#'){ // Si il y a un mur en dessous
                return(map_loaded);
            }
            if ((p_new_array[pos+width]=='$' && p_new_array[pos+2*width]=='$')
              ||(p_new_array[pos+width]=='$' && p_new_array[pos+2*width]=='*') 
              ||(p_new_array[pos+width]=='*' && p_new_array[pos+2*width]=='$') 
              ||(p_new_array[pos+width]=='*' && p_new_array[pos+2*width]=='*')){//s'il y a 2 caisses au dessus
                return(map_loaded);
            }
            if ((p_new_array[pos+width]=='$' && p_new_array[pos+2*width]=='#') //s'il y a une caisse au niveau +1 et un mur au niveau +2
              ||(p_new_array[pos+width]=='*' && p_new_array[pos+2*width]=='#')){
                return(map_loaded);
            }
             // On vérifie d'abord s'il y a une caisse à déplacer

            if (p_new_array[pos+width]=='$' || p_new_array[pos+width]=='*'){
                if (p_new_array[pos+2*width]=='.'){
                    p_new_array[pos+2*width]='*';
                }   
                else {
                    p_new_array[pos+2*width]='$';
                }

                if (p_new_array[pos+width]=='*'){
                    p_new_array[pos+width]='.';
                }
                else{
                    p_new_array[pos+width]=' ';
                }   
            }

             //On change le char en position du joueur 

            if (p_new_array[pos]=='+'){
                p_new_array[pos+width]='@';
                p_new_array[pos]='.';

            }
            else if (p_new_array[pos+width]=='.'){
                p_new_array[pos+width]='+';
                p_new_array[pos]=' ';
            }
            else {
                p_new_array[pos+width]='@';
                p_new_array[pos]=' ';
            } 
            break;




        case 'W':
        if (p_new_array[pos-1]=='#'){ // Si il y a un mur en dessous
                return(map_loaded);
            }
            if ((p_new_array[pos-1]=='$' && p_new_array[pos-2]=='$')
              ||(p_new_array[pos-1]=='$' && p_new_array[pos-2]=='*') 
              ||(p_new_array[pos-1]=='*' && p_new_array[pos-2]=='$') 
              ||(p_new_array[pos-1]=='*' && p_new_array[pos-2]=='*')){ //s'il y a 2 caisses à gauche
                return(map_loaded);
            }
            if ((p_new_array[pos-1]=='$' && p_new_array[pos-2]=='#') //s'il y a une caisse au niveau -1 et un mur au niveau -2
              ||(p_new_array[pos-1]=='*' && p_new_array[pos-2]=='#')){
                return(map_loaded);
            }

            // On vérifie d'abord s'il y a une caisse à déplacer

            if (p_new_array[pos-1]=='$' || p_new_array[pos-1]=='*'){
                if (p_new_array[pos-2]=='.'){
                    p_new_array[pos-2]='*';
                }   
                else {
                    p_new_array[pos-2]='$';
                }

                if (p_new_array[pos-1]=='*'){
                    p_new_array[pos-1]='.';
                }
                else{
                    p_new_array[pos-1]=' ';
                }   
            }


            //On change le char en position du joueur 

            if (p_new_array[pos]=='+'){
                p_new_array[pos-1]='@';
                p_new_array[pos]='.';

            }
            else if (p_new_array[pos-1]=='.'){
                p_new_array[pos-1]='+';
                p_new_array[pos]=' ';
            }
            else {
                p_new_array[pos-1]='@';
                p_new_array[pos]=' ';
            } 
            break;


        case 'E':
        if (p_new_array[pos+1]=='#') {// Si il y a un mur à droite
                return(map_loaded);
            }
            if ((p_new_array[pos+1]=='$' && p_new_array[pos+2]=='$')
              ||(p_new_array[pos+1]=='$' && p_new_array[pos+2]=='*') 
              ||(p_new_array[pos+1]=='*' && p_new_array[pos+2]=='$') 
              ||(p_new_array[pos+1]=='*' && p_new_array[pos+2]=='*')){ //s'il y a 2 caisses à droite
                return(map_loaded);
            }
            if ((p_new_array[pos+1]=='$' && p_new_array[pos+2]=='#') //s'il y a une caisse au niveau +1 et un mur au niveau +2
              ||(p_new_array[pos+1]=='*' && p_new_array[pos+2]=='#')){
                return(map_loaded);
            }

            // On vérifie d'abord s'il y a une caisse à déplacer

            if (p_new_array[pos+1]=='$' || p_new_array[pos+1]=='*'){
                if (p_new_array[pos+2]=='.'){
                    p_new_array[pos+2]='*';
                }   
                else {
                    p_new_array[pos+2]='$';
                }

                if (p_new_array[pos+1]=='*'){
                    p_new_array[pos+1]='.';
                }
                else{
                    p_new_array[pos+1]=' ';
                }   
            }


            //On change le char en position du joueur 

            if (p_new_array[pos]=='+'){
                p_new_array[pos+1]='@';
                p_new_array[pos]='.';

            }
            else if (p_new_array[pos+1]=='.'){
                p_new_array[pos+1]='+';
                p_new_array[pos]=' ';
            }
            else {
                p_new_array[pos+1]='@';
                p_new_array[pos]=' ';
            } 
            break;

        }
    return(new_map);
}

這是加載函數;

char* Array_Creator(char* filename){
    FILE* p_file = fopen(filename, "r");
    char* p_array = NULL;
    if (p_file == NULL) {
        exit(EXIT_FAILURE);
    }
    else{   
        size_t size=1;
        int c;
        while (getc(p_file)!=EOF) {
            size++;
        }
        fseek(p_file,0,SEEK_SET);
        while ((c=getc(p_file))!='\n' && c!=EOF) { //on se débarasse de la première ligne
            size--;
        }
        p_array=(char*)malloc(sizeof(char)*size);
        if (p_array!=NULL) { //si jamais le malloc ne fonctionne pas
            for(size_t i=0; i<size-1; i++) {
            p_array[i]=(char)getc(p_file);


            if (p_array[i] == '\n') { // si le caractère est une nouvelle ligne, on s'en sépare
                i--; // on ajuste alors la taille et l'indice
                size--;
                }
            }
            p_array[size-1]='\0';
        }
        fclose(p_file);
    }
    return p_array;
}

//La fonction Dimensions permet de récupérer les dimensions d'une map

couple Dimensions(char *p_char){

    FILE *p_file = NULL;
    p_file=fopen(p_char, "r");

    if (p_file == NULL) {
        fprintf(stderr, "Cannot read file %s!\n", p_char);
        exit(EXIT_FAILURE);
    }


    couple dim={0,0}; //la structure couple est déf dans le loader.h

    int width         = 0;
    int height        = 0;
    int fscanf_result = 0;

    fscanf_result = fscanf(p_file, "%d %d\n", &width, &height);
    if (fscanf_result != 2) {
        fprintf(stderr, "First line is not syntactically correct!\n");
        exit(EXIT_FAILURE);
    }

    dim.x=width;
    dim.y=height;
    fclose(p_file);
    return dim;
}


//La fonction Load est celle demandée et permettant de charger une carte ; elle utilise Dimensions et Array_Creator

map* load(char *filename){
    map* map_loaded=malloc(sizeof(map)); //on alloue dynamiquement la map

    //Dans un premier temps on récupère les dimensions

    couple map_dim={0,0};  
    map_dim=Dimensions(filename);
    map_loaded->width=map_dim.x;
    map_loaded->height=map_dim.y;

    //Dans un second temps on définit le tableau 1D contenant l'ensemble des éléments de la map

    char* p_char=Array_Creator(filename);
    map_loaded->p_char=p_char;
    return map_loaded;
}

命令行是,例如: ./replay ./data/soko.in 4 7"NSWSESNWW"

move功能中,應該改變所有

return(map_loaded); => return(new_map);

當移動函數返回 map_loaded 時,它會釋放兩次 map_loaded

代碼分析

map *replay(map *map_loaded, int length, char *plan)
{
    map *new_map = move(map_loaded, plan[0]); // [2] set new_map to map_loaded
    map *old_map = deep_copy(new_map);
    for (int i = 1 ; i < length ; i++) {
        free(new_map->p_char);
        free(new_map);                        // [3] new_map be freed, 
                                              // equivalent map_loaded be freed
        new_map = move(old_map, plan[i]);
        ...
    }
    ...
    return new_map;
}

int main(int argc, char *argv[])
    ...
    map *map_loaded = load(argv[1]);         // [1] map_loaded be malloc
    ...
    map *new_map = replay(map_loaded, length, plan);
    ...
    free(map_loaded->p_char);
    free(map_loaded);                        // [4] map_loaded be freed
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM