首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >评论2 free时出现分割错误(Sokoban游戏)

评论2 free时出现分割错误(Sokoban游戏)
EN

Stack Overflow用户
提问于 2019-03-18 18:07:02
回答 1查看 73关注 0票数 2

从昨天开始我一直在尝试解决分段错误,这让我抓狂……所以我有一个程序,我用它来实现sokoban游戏,其中给出了一张地图,并给出了一个类似"NSEW“的计划,意思是我希望玩家向北移动,然后向南移动,然后等等……在给定的地图上。

代码语言:javascript
复制
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是一种定义如下的结构:

代码语言:javascript
复制
typedef struct map map;
struct map{
    int width;
    int height;
    char* p_char;
};

move函数执行一次移动;

deep_copy创建了一个映射结构的深层副本:

代码语言:javascript
复制
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程序时:

代码语言:javascript
复制
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;
}

我得到了一个分段错误...但如果我这么做了:

代码语言:javascript
复制
//free(old_map->p_char);
//free(old_map);

一切都很完美!!我真的不明白为什么..。问题是,如果我做了这些免费的valgrind告诉我,我没有那么多的免费分配,这是正常的…

我真的很感谢任何人的帮助。如果你有足够的勇气读到这一点,首先要感谢你!

编辑:这是我的移动函数;

代码语言:javascript
复制
   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);
}

这里是load函数;

代码语言:javascript
复制
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“

EN

回答 1

Stack Overflow用户

发布于 2019-03-19 10:56:49

move函数中,应将所有

代码语言:javascript
复制
return(map_loaded); => return(new_map);

当map_loaded函数返回map_loaded时,它将被释放两次

代码分析

代码语言:javascript
复制
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
    ...
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55218871

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档