因为不良的malloc程序崩溃()语法 [英] program crashes because of bad malloc() syntax

查看:157
本文介绍了因为不良的malloc程序崩溃()语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct nouedls
{
    char * path;
    char * name;
    struct nouedls * suiv;
};

typedef struct nouedls nouedls;
typedef nouedls * listes;

listes ajouters(listes ls, char * path , char* name)
{

    nouedls * x=malloc(sizeof(x));
    listes auxl;
    x->path=malloc(strlen(path)*sizeof(char));

    x->name=malloc(strlen(name)*sizeof(char));


    strcpy(x->path,path);

    strcpy(x->name,name);


    x->suiv=NULL;


    if (ls==NULL){

            ls=x;
            }
    else
    {
        auxl=ls;
        while(auxl->suiv!=NULL)
        {
            auxl=auxl->suiv;
        }
        auxl->suiv=x;

    }
    return(ls);
}

void makepath(char* path){

    char ch2 [400];
    int i=0 ;
    int k=0 ;
    while (path[i]!='\n')
    {
    if (path[i]!='\\')
    {
    ch2[k]=path[i] ;
    i++ ;
    k++ ;
    }
    else
    {
    ch2[k]=path[i] ;
    k++ ;
    ch2[k]='\\' ;
    k++ ;
    i++ ;
    }
    }
    ch2[k]='\0' ;
}




void menu (char*path){
    FILE *fp;
    char *fname = "MyScript.bat";
    FILE *f ;
    listes menu=malloc(sizeof(menu)) ;
    char upath [400];
    char name [260] ;
    menu=NULL ;
    if ((fp = fopen(fname, "wt")) == NULL)
    {fatal("Cannot open script file");}
    makebatfd(fp, "MyDirectory",path);
    if (fclose (fp))
    {fatal("Cannot close script file");}
    system("MyScript.bat>menu.txt");//make the menu .txt file
    nom(path) ;//makes the name.txt file
    if ((fp = fopen("menu.txt", "r+")) == NULL)
    {fatal("Cannot open menu file");}
    if ((f = fopen("name.txt", "r+")) == NULL)
    {fatal("Cannot open name file");}
printf("path") ;
while (fgets(upath,400,fp)!= NULL )
    {

    makepath(upath) ;//it handles the character '\' 

    fgets(name,260,f) ;
printf("%s",upath) ;
menu=ajouters(menu,upath,name) ;
printf("path") ;
    }


    if (fclose (f))
    {fatal("Cannot close name file");}
    if (fclose (fp))
    {fatal("Cannot close menu file");}


}

菜单函数创建一个文件 menu.txt 其中包含的文件路径和其他文件 name.txt 包含这些文件的名称。我希望把包含在使用功能列表中的这些文件中的信息 ajouters 它的工作原理,但在一定行的崩溃(我只放了我怀疑的功能,因为别人很好地工作)

the menu function creates a file menu.txt which contains paths of files and another file name.txt contains names of those files . i wanted to put the information contained in those files in a list using the function ajouters it works but at a certain line it crashes ( i put only the function that i suspect , because the others work perfectly )

推荐答案

我想,你需要改变

第一点:

nouedls * x=malloc(sizeof(x));

nouedls * x=malloc(sizeof(*x));   // *x == struct nouedls, x == struct nouedls *

2点:

x->path=malloc(strlen(path)*sizeof(char));

x->path=malloc(strlen(path) + 1);    //+ 1 to hold the terminating null, sizeof(char) == 1 always.

和亦同。

此外,你已经检查返回值的malloc()使用返回的指针之前的成功。

Also, you've to check the return value of malloc() for success before using the returned pointer.

这篇关于因为不良的malloc程序崩溃()语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆