Thread: Why do I get segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    61

    Why do I get segmentation fault

    I need to parse data from a csv file but for some reason I'm getting a segmentation fault error. I have exactly 5 comma separated fields on each line so I wanted to use an array of pointers to hold each field.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX 300
    
    
    typedef struct{
        char *name, *county;
        float grade;
        unsigned long int year, number;
    }info;
    
    
    int main(int argc, char **argv)
    {
        FILE *fin = fopen("lab3.txt", "r");
        if(!fin)
        {
            fprintf(stderr, "ERROR OPENING THE FILE.\n");
            exit(1);
        }
        char buf[MAX];
        while(fgets(buf, MAX, fin))
        {
            buf[strcspn(buf,"\n")] = '\0'; // remove the trailing newline
            char *fields[6], *word = strtok(buf, ",");
            while(word)
            {
                int i = 0;
                fields[i] = word;
                info *array = (info *)malloc(sizeof(info));
                array->name = (char *)malloc(sizeof(char));
                array->county = (char *)malloc(sizeof(char));
                array->name = strdup(fields[2]);
                array->county = strdup(fields[3]);
                array->grade = atof(fields[0]);
                array->year = atoi(fields[1]);
                array->number = atoi(fields[4]);
                printf("Year : %s | Grade : %s | Number: %s", fields[1], fields[0], fields[5]);
                free(array);
                //printf("Word : %s\n", fields[i]);
                word = strtok(NULL,",");
                i++;
                free(array->name);
                free(array->county);
    
    
    
    
            }
        }
        fclose(fin);
        return 0;
    }
    I'm allocation memory to array, array->name and array->county so I don't know why this is happening. I also used gdb and found the problem at line 36 but everything seems okay. Any ideas?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,275
    malloc(sizeof(char)) allocates space for exactly one char, so if you're trying to use it as a string, it can only store an empty string. But then you overwrite it with strdup, so you end up with a small memory leak.

    Later, you call free(array), then you access the members of what array points to in order to free. That's the wrong order: free(array) should come last.
    Last edited by laserlight; 03-03-2021 at 01:32 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    61
    So should I initialize with strlen(word)+1 * sizeof(char)?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,275
    You probably want to declare the array pointer earlier: makes no sense to declare it in that inner loop in the middle of parsing since you want to parse the current item and assign it to the relevant element of array rather than assign all at once on each iteration before the parsing is complete.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation Fault
    By DeanWinchester in forum C Programming
    Replies: 4
    Last Post: 05-28-2012, 11:23 AM
  3. Segmentation fault! Why?
    By cvrcak in forum C Programming
    Replies: 4
    Last Post: 06-25-2008, 07:24 PM
  4. Segmentation Fault
    By Terran in forum C++ Programming
    Replies: 2
    Last Post: 06-04-2008, 07:39 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread