Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的csv_loader()函数中的Segfault_C_Csv_Segmentation Fault - Fatal编程技术网

我的csv_loader()函数中的Segfault

我的csv_loader()函数中的Segfault,c,csv,segmentation-fault,C,Csv,Segmentation Fault,我正试图用C编写一个csv解析器,但每次我在这个函数中得到一个segfault。我不知道怎么修理它 char*** csv_loader(char *filename){ FILE* file_xx; file_xx = fopen(filename, "r"); if(file_xx==NULL){ printf("Failed to open File, no such file or directory!\n"); return 0; } int c_=0; int **l

我正试图用C编写一个csv解析器,但每次我在这个函数中得到一个segfault。我不知道怎么修理它

char*** csv_loader(char *filename){
FILE* file_xx;
file_xx = fopen(filename, "r");
if(file_xx==NULL){
    printf("Failed to open File, no such file or directory!\n");
    return 0;
}
int c_=0;
int **linenumbers;
int l=lines(filename);
linenumbers=malloc(sizeof(int)*l);
char*** loaded_csv;
int counter_line=0;
int counter_row=0;
loaded_csv=malloc(sizeof(char **) *l);
loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);
if(NULL==loaded_csv){
    printf("Failed to initialize 'char** loaded_csv'!\n");
    return 0;
}
int c_c=0;
int *cm=get_column_map(filename);
for(c_c=0;c_c<l;c_c++){
    loaded_csv[c_c]=malloc(sizeof(char *)*cm[c_c]);
}
while(c_!=EOF){
    c_=getc(file_xx);
    if(c_=='\n'){
        linenumbers[counter_line][cm[counter_line]]=counter_row+2;
        loaded_csv[counter_line][cm[counter_line]]=malloc(counter_row*sizeof(char));
        if(NULL == loaded_csv[counter_line][cm[counter_line]]){
        return 0;
        }
        loaded_csv[counter_line][counter_row]='\0';
        counter_row=0;
        counter_line++;
    }else{
        if(c_==','){
            counter_row=0;
        }else{
            counter_row++;
        }
    }
}
fclose(file_xx);
FILE*fgetsread;
fgetsread=fopen(filename, "r");
int ident, ident_c;
for(ident=0;ident<l;ident++){
    for(ident_c=0;ident_c<cm[ident];ident_c++){
        fgets(loaded_csv[ident][ident_c], linenumbers[ident][ident_c], fgetsread);
        loaded_csv[ident][ident_c][linenumbers[ident][ident_c]-2]='\0';
    }
}
fclose(fgetsread);
free(linenumbers);
return loaded_csv;
}
有人知道有什么问题吗?我对C还是个新手,不管怎样,试着去理解malloc的东西


PS:其他函数在这里:

因此,您已经在前面的行中分配了空间:

loaded_csv=malloc(sizeof(char **) *l);
这很好,但是
加载的\u csv[0]
尚未初始化到您自己的位置。所以,当你做下面这行的时候

loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);
您正试图设置一个位于某个随机位置的变量(只要
加载的地方\u csv[0]
恰好在此时)


如果您想触摸
加载的\u csv[0][0]
,您必须确保
加载的\u csv[0]
首先指向有效内存(可能通过
malloc
为其分配内存,然后再为
加载的\u csv[0][0]
分配内容)

这行代码有问题:

loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);  
这表明您没有正确分配和初始化加载的\u csv[0][0]

下面是一个如何初始化和使用char***变量的示例:

#include <windows.h>
#include <ansi_c.h>
char *** Create3D(int p, int c, int r);
int main(void)
{
    char ***pppVar;

    pppVar = Create3D(10,10,10);  
    //test pppVar;
    strcpy(pppVar[0][0], "asdfasf");
    strcpy(pppVar[0][1], "the ball");

    return 0;
}   

char *** Create3D(int p, int c, int r) 
{
    char *space;
    char  ***arr;
    int    x,y;

    space = calloc (p*c*r*sizeof(char),sizeof(char));
    arr = calloc(p * sizeof(char **), sizeof(char));
    for(x = 0; x < p; x++)
    {
        arr[x] = calloc(c * sizeof(char *),sizeof(char));
        for(y = 0; y < c; y++)
        {
            arr[x][y] = ((char *)space + (x*(c*r) + y*r));
        }
    }
    return arr;
}  
#包括
#包括
字符***Create3D(int p、int c、int r);
内部主(空)
{
char***pppVar;
pppVar=Create3D(10,10,10);
//测试pppVar;
strcpy(pppVar[0][0],“asdfasf”);
strcpy(pppVar[0][1],“球”);
返回0;
}   
字符***Create3D(整数p、整数c、整数r)
{
字符*空间;
字符***arr;
int x,y;
space=calloc(p*c*r*sizeof(char),sizeof(char));
arr=calloc(p*sizeof(char**),sizeof(char));
对于(x=0;x

确保适当地跟踪和释放(x)。

谢谢,我做得太晚了,我在这一行分配了它:因为(c_c=0;c_cedit:我自己看到了,这是错误的行号分配。我不太喜欢calloc(),因为它将固定内存块直接设置为0,我喜欢malloc()中的动态因为你只使用你需要的内存。但是如果我错了,请纠正我…calloc和malloc都分配了固定数量的内存,但malloc不保证分配的内存的内容。也就是说,分配的空间有不确定的值。如果你不小心,这可能会导致问题。也就是说,两者都工作正常,calloc只是一个首选项给我来个电话。
#include <windows.h>
#include <ansi_c.h>
char *** Create3D(int p, int c, int r);
int main(void)
{
    char ***pppVar;

    pppVar = Create3D(10,10,10);  
    //test pppVar;
    strcpy(pppVar[0][0], "asdfasf");
    strcpy(pppVar[0][1], "the ball");

    return 0;
}   

char *** Create3D(int p, int c, int r) 
{
    char *space;
    char  ***arr;
    int    x,y;

    space = calloc (p*c*r*sizeof(char),sizeof(char));
    arr = calloc(p * sizeof(char **), sizeof(char));
    for(x = 0; x < p; x++)
    {
        arr[x] = calloc(c * sizeof(char *),sizeof(char));
        for(y = 0; y < c; y++)
        {
            arr[x][y] = ((char *)space + (x*(c*r) + y*r));
        }
    }
    return arr;
}