Search code examples
csegmentation-faultbinaryfilesdynamic-arrays

Seg fault while trying to print data stored from binary file into dynamic array


I have a binary file that I can view as text that has words in it stored in this way: entry oneentry twoentry three (so seperate entries are consecutive but a single entry can contain two words separated by space)

I have this code:

        char **data;
        int start = 0;

        data = malloc(all_names * sizeof(char*));

        fd=open(argv[1],O_RDWR|O_CREAT,S_IRWXU);
        for(i=0; i<all_names; i++){
            data[i] = malloc((MAX_SIZE+1)*sizeof(char));

            int end = atoi(positions[i]);
            read(fd,&data[i],(end-start));
            start = end;
        }

        for(i=0; i<all_names; i++){
            printf("%s\n", data[i]);
        }

Positions array contains the location of the start of every string, apart from the first one.

eg. if positions[0] = 9, that means the first entry had 9 characters, and the first character of the second entry is byte number 9.

I get a segmentation fault that seems to happen right after trying to print the array, but I cannot figure out if it is caused by false printing or the words not being stored correctly in the first place.

Now, I had trouble creating my pos array and asked a similar question here. After that being solved and this not working, I tried this version that looks more like the answer I got to my other question:

fd=open(argv[1],O_RDWR|O_CREAT,S_IRWXU);
    for(i=0; i<all_names; i++){
        positions[i] =malloc((MAX_SIZE+1)*sizeof(char));
        int end = atoi(positions[i]);

            for(j=0;j<(end-start) ;j++){
                read(fd,&data[i][j],1);
            }
        data[i][j]=0;
        printf("%s\n",data[i]);
     }

And the same code when printing, getting the same seg fault at the same point.

I tried printing character by character:

start = 0;
        for(i=0; i<all_names; i++){
            int end = atoi(positions[i]);
            for(j=0;j<(end-start) ;j++){
                printf("%c",data[i][j]);
            }
            printf("\n");
        }

Still the same problem. Thanks for any help, and if you feel the need to vote negatively please explain why so I can improve it. If more info is needed I will provide it.


Solution

  • As stated in "Some programmer dude"'s answer, removing the & from &data[i] will eliminate the seg fault. I saw your comment and I think you are simply not combining the correct parts of your code.

            data = malloc(all_names * sizeof(char*));
    
            fd=open(argv[1],O_RDWR|O_CREAT,S_IRWXU);
            for(i=0; i<all_names; i++){
                data[i] = malloc((MAX_SIZE+1)*sizeof(char));
    
                int end = atoi(positions[i]);
                lseek(fd,0,start);
                read(fd,data[i],(end-start));
                start = end;
    
            }
    
            start = 0;
            for(i=0; i<all_names; i++){
                int end = atoi(positions[i]);
                for(j=0;j<(end-start) ;j++){
                    printf("%c",data[i][j]);
                }
                printf("\n");
            }
    

    This should work just fine.