0

I'm trying to read a file that contains a five letter word on each line for 12972 lines. I'm not sure why I'm getting this error even with freeing storage.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *file;
    file = fopen("words.txt", "r");                                                  // Opens file

    char** gssArr = (char**)malloc(12972 * sizeof(char*));       // Allocates memory for 2d array

    for(int i = 0; i < 12972; i++)
    {
        gssArr[i] = (char*)malloc(5 * sizeof(char));
    }

    char word[6];                                    // Current word being looked at from file
    int current = 0;                                    // Used for indexing x coordinate of 2d array
    while(fgets(word,6,file) != NULL)   // Not at end of file
    {
        if(word[0] != '\n')                  // Not at end of line
        {
            for(int j = 0; j < 5; j++)           // Loops through all 5 letters in word, adding them to gssArr
            {
                gssArr[current][j] = word[j];
            }
        }
        current++;                                  // Index increase by 1
    }
    fclose(file);                         // Close file, free memory
    free(gssArr);
14
  • 1
    Where do you get the segmentation fault? Commented Feb 24, 2022 at 21:27
  • Freeing memory doesn't prevent segmentation fault. It prevents memory leaks. You have a memory leak because you never free all the gssArr[i]. Commented Feb 24, 2022 at 21:29
  • looks like you are using a C++ compiler, you should not cast the return value of malloc Commented Feb 24, 2022 at 21:29
  • 1
    @AndersK You have it backwards. You need the cast with C++, you don't need it with C. Commented Feb 24, 2022 at 21:30
  • personally i would be a bit more generous when reading a line from the file instead of exactly 6 chars. use a 128 byte buffer or something, then retrieve what you need. Commented Feb 24, 2022 at 21:30

2 Answers 2

1

FYI - your reader loop - you may want to make sure that your current index is not going beyond 12971.

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

Comments

0

Your problem is here:

current++; 

Why?

Because it's done even when you are at the end of the line. One solution is to move it inside the if statement but instead I'll recommend that you use fgets with a much larger buffer.

Details

If every line holds a five letter word then

fgets(word,6,file)

will read the five letters and zero terminate it. Then the next fgets will just read the "newline". Still you increment the index counter and in the end, you write outside allocated memory.

Try

while(fgets(word,6,file) != NULL)   // Not at end of file
{
    printf("current=%d\n", current);

and you see the problem.

Comments

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.