#Segmentation Fault (core dumped) error

1 messages · Page 1 of 1 (latest)

stone walrus
#

I have a C file that reads earthquake data and adds it into a LinkedList of structures. It works fine locally, but I have memory issues with it. Could someone take a look at my methods and let me know if there are any glaring issues?

fathom iceBOT
#

<@&987246683568103514> please have a look, thanks.

fathom iceBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

stone walrus
#
region_header_t *readData(char *filename, int *numberOfRegions) {
    FILE *file;
    char line[100];
    region_header_t *header = NULL, *current = NULL;
    equake_data_t *data = NULL, *currentData = NULL;
    *numberOfRegions = 0;

        // Create region header and add to linked list
        region_header_t *regionHeader = malloc(sizeof(region_header_t));
        regionHeader->region_name = strdup(line);
        regionHeader->data = NULL;
        regionHeader->next = NULL;

        // Read data file and add to linked list
        while (fgets(line, sizeof(line), dataFile)) {
            equake_data_t *quakeData = malloc(sizeof(equake_data_t));
            quakeData->next = NULL;
            int x, y, z;
            char s[12] = {};
            char t[50] = {};
            float l, m, n, o;
            sscanf(line, "%d %d %d %s %f %f %f %f %s",
                &x, &y, &z, s, &l, &m, &n, &o, t);
            quakeData->year = x;
            quakeData->month = y;
            quakeData->day = z;
            quakeData->timeOfQuake = (char *)malloc(sizeof(char)*(strlen(s) + 1));
            strcpy(quakeData->timeOfQuake, s);
            quakeData->latitude = l;
            quakeData->longitude = m;
            quakeData->magnitude = n;
            quakeData->depth = o;
            quakeData->location = (char *)malloc(sizeof(char)*(strlen(t) + 1));
            strcpy(quakeData->location, t);

            if (!data) {
                data = quakeData;
                currentData = data;
            } else {
                currentData->next = quakeData;
                currentData = currentData->next;
            }
        }

        regionHeader->data = data;
        data = NULL;
        currentData = NULL;
        (*numberOfRegions)++;
    }

    // Clean up and return header
    fclose(file);
    return header;
}
#

i removed a couple lines because of discords character limit

#

sorry if this is really complex i don't even know where to start myself

stone walrus
#

uhhh i fixed most of the memory leaks but there are 4 more hidden throughout the 370 lines of my code so i think itll take more than just staring at it to fix it 😭

stone walrus
#

but there are a few more and i can't figure out where they are

#

or what variable types to use them on