c - 评论 2 免费时出现段错误(推箱子游戏)

标签 c segmentation-fault fault

从昨天开始,我一直在尝试解决一个段错误,这让我发疯了...... 所以我得到了我用来实现推箱子游戏的程序,其中给出了一张 map ,并给出了一个像“NSEW”这样的计划,这意味着我希望玩家向北移动,然后向南移动,然后等等。 . 在给定的 map 上。

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;
};

移动函数做一个 Action ;

deep_copy 对 map 结构进行深度复制:

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);
}

发生的情况是,如果我保留这样的代码,如果选择的长度(我希望玩家进行的移动次数)太高(取决于 map 和移动),则在执行回放时。 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);

一切正常!! 我真的不明白为什么...问题是如果我执行那些 free valgrind 告诉我我没有 allocs 那么多的 free,这是正常的...

如果能提供任何帮助,我将不胜感激... 在此之前,如果您有足够的勇气阅读我的内容,请提前致谢!

编辑: 这是我的移动功能;

   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);

当move函数返回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
    ...
}

关于c - 评论 2 免费时出现段错误(推箱子游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55218871/

相关文章:

c - 将指向一种类型的指针转​​换为指向另一种类型的指针的限制

c - GET 请求只获取空字符串

objective-c - SIGSEGV 崩溃,没有任何直接跟踪我的应用程序代码

iphone - 核心数据。 ExecuteFetchRequest 出现故障 NSManagedObject(不在 RAM 中)

c - 非可移植指针转换

c - malloc 中的段错误

包含原始指针的对象的 C++ 智能指针

operating-system - 操作系统对页面错误的响应

ios - 关系故障(核心数据)

c - 打开/dev/mem - 不允许操作