0

I'm trying to write a csv parser in C, but every time I get a segfault in this function. I don't know how to fix it.

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

The Debugger says it's this line:

    loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);

Does anyone know what's the bug? I'm yet new to C and anyway try to understand the malloc thing...

PS: the other functions are here: http://pastebin.com/VQZ4d5UU

2 Answers 2

1

So, you've allocated space on the line right before:

loaded_csv=malloc(sizeof(char **) *l);

That is fine and dandy, but loaded_csv[0] isn't yet initialized to somewhere you own. So, when you do the following line

loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);

you are trying to set a variable located in some random location (wherever loaded_csv[0] happens to be right then).

If you want to touch loaded_csv[0][0], you'll have to make sure that loaded_csv[0] is pointing to valid memory first (probably by allocating memory for it via malloc before you allocate something for loaded_csv[0][0].)

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I did it too late, I allocated it right at this line: for(c_c=0;c_c<l;c_c++){ loaded_csv[c_c]=malloc(sizeof(char *)*cm[c_c]); } now I switched it right above the error line and the next error appears at the line: linenumbers[counter_line][cm[counter_line]]=counter_row+2;
edit: I saw it myself, it's the false allocation of linenumbers
0

You've got a problem with this line:

loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);  

Which suggest you are not allocating and initializing loaded_csv[0][0] right.

Here is an example of how to initialize and use a char ***var:

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

Be sure to keep track of and free(x) appropriately.

2 Comments

I don't like calloc() very much because it sets a fixed memory block directly to 0, I like the dynamic in malloc() because you use only the memory you need. But correct me if I'm wrong...
Both calloc and malloc allocate a fixed amount of memory, but malloc does not guarantee the allocated memory's contents. i.e., The space allocated has indeterminate value. This can lead to problems if you are not careful. That said, both work fine, calloc is just a preference for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.