-4

Having trouble with segmentation fault 11 error.

/* Create main function */
int main()
{
    char line[9999999];
    FILE *fp;
    fp = fopen("file.txt", "r");
    while (fgets(line, 9999999, (FILE*)fp) != NULL)
    {
        if (strstr(line, "word") != NULL)
        {
            printf("%s", line);
        }
    }
    fclose(fp);
    return 0;
}

Already tried to reduce 9999999 to 256 but this also didn't work..

Thanks in advance

Phillsss
  • 1
  • 1
  • 3
  • 1
    When you reduced from 9999999 to 256? did you edit both `line[9999999];` and `fgets(line, 9999999,`? Easier to use/maintain `while (fgets(line, sizeof line , fp) != NULL)` – chux - Reinstate Monica Dec 22 '15 at 20:30
  • 1
    *Which* `9999999 to 256` did you try to reduce? Both of them? – Weather Vane Dec 22 '15 at 20:31
  • I'd have problems keeping it the same number of `9`s in both integers... – cadaniluk Dec 22 '15 at 20:32
  • Yes, I did change both of them. Still got the error. – Phillsss Dec 22 '15 at 20:32
  • 2
    Aside: you don't need to cast `(FILE*)fp` since the argument for `fgets` is ... `FILE*`. – Weather Vane Dec 22 '15 at 20:33
  • 2
    Does your `file.txt` file exist? – dragosht Dec 22 '15 at 20:33
  • 4
    You dont check the return value of `fopen`. is `fp` definitely not `NULL`? – Jimbo Dec 22 '15 at 20:33
  • 1
    Also such a large array on the stack is generally a bad idea... the stack isn't that big. Better to dynamically allocate mem on the heap if you want to use such large buffers (i.e. use `malloc`) – Jimbo Dec 22 '15 at 20:37
  • If I check the fp pointer to make sure the file exists and reduce the size of the `line` variable so it fits on the stack it works for me... – Jimbo Dec 22 '15 at 20:40
  • 1
    Everyone, thank you so much for your response and help:) Dragosht was right, apparently I did not have file permission to open it. And due to the segmentation error I did not look into that but rather into finding mistakes in my script:) – Phillsss Dec 22 '15 at 20:40
  • http://stackoverflow.com/questions/571945/getting-a-stack-overflow-exception-when-declaring-a-large-array – Lundin Dec 23 '15 at 08:04

2 Answers2

1

I think there are 2 potential problems

Problem 1 is the stack. The stack is a (relatively) small bit of memory used per thread to keep track of function automatically scoped variables, values in and out and such. It is not meant for large amounts of data. It is quite possible that your line buffer is just too large for the stack!

If you want a buffer that large it is best to use malloc() to allocate on the heap. The heap is large and if the allocation fails you can detect it and handle it appropriately.

Problem 2 is not checking the value of fp. fopen() may return NULL if it fails to open the file. If this is the case and you pass fp to fgets() you'll get a seg fault.

When I addressed these two problems on my machine, your program worked...

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

#define BUFSIZE 99999

int main()
{
    char *line = malloc(BUFSIZE);
    FILE *fp = fopen("file.txt", "r");
    if(!line || !fp) {
       printf("OOPS");
       free(line);
       return 1;
    }

    while (fgets(line, BUFSIZE, fp) != NULL)
    {
        if (strstr(line, "word") != NULL)
        {
            printf("%s", line);
        }
    }
    fclose(fp);
    free(line);
    return 0;
}

Note fgets() reads at most one less than the size parameter and will append a null terminator for you.

Jimbo
  • 4,352
  • 3
  • 27
  • 44
1

In my tests under cygwin + gcc 4.9.3, my program crashes when the size of the array is 9999999 but not 999999.

The culprit here is that the array is too large to fit in stack memory.

Simpler program that crashes:

#define SIZE 9999999
int main()
{
    char line[SIZE];
    line[SIZE-1] = '\0';
    return 0;
}

It works fine when I change SIZE to:

#define SIZE 999999

Creating large arrays in stack memory is a well known problem. See Why does a large local array crash my program?.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270