1

Everytime I try to do this I get a segmentation error. It's been really bothering me because this was a pretty difficult assignment and its late because I can't fix this one thing. I get a segmentation fault when I try to run parseInputStream. I've tried changing the file pointer to a pointer to a file pointer but I still get the segmentation fault. The console prints all of the ns but none of the ws. Help please.

void parseInputStream(FILE *file, FILE *infile){
    printf("w\n");
    char string[9999999];
    while(fscanf(infile, "%s", &string)!= EOF){
            printf("w\n");
            if(string[0] == ';'){
            fgets(string, 9999999, infile);
            continue;
            }
            parseInput(file, infile, string);
}



int main(int argc, char* argv[]){
    printf("n\n");
    FILE *file = fopen("intest.asm", "w");
    printf("n\n");
    FILE *infile = fopen("intest.j", "r");
    printf("n\n");
    parseInputStream(file, infile);
    fclose(infile);
    fclose(file);
    return 0;
}
  • 1
    the stack maximum allowed size is usually around 8Mb in size, at least under a GNU/linux distribution, it may vary, but it's probably not gonna be that large. – user2485710 Dec 13 '13 at 23:15
  • Using `scanf` and `fgets` on the same stream is bound to [cause problems](http://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf). Also: you're passing `&infile` (a `FILE **`) to `fgets`, your compiler should be complaining about that. – Kninnug Dec 13 '13 at 23:17

2 Answers2

3
char string[9999999];

tries to declare just under 10Mb on the stack. This is presumably too large and is causing your stack to overflow.

The easiest solution is to move string to another area of memory. You could move it to have static duration:

static char string[9999999];

or you could allocate it dynamically:

char* string = malloc(9999999);
if (string==NULL) {
    // no memory, exit
}
// your code as before
free(string);

Also, as noted by Pyrce, allocating a 10 meg buffer like this is probably a bad sign. If you need to read an entire file into memory, you could check its size first then allocate a buffer that is exactly the right size. If you just want to read individual words, you can probably assume a smaller maximum and tell fscanf the size of your buffer to avoid any risk of writing beyond it

char string[100];
fscanf("%99s", string);
simonc
  • 41,632
  • 12
  • 85
  • 103
  • You might want to also mention that it's probably a bad design to allocate a 10MB buffer for loading an arbitrary file – Pyrce Dec 13 '13 at 23:14
  • Yea, that was it. In retrospect that was an awful idea. I just wanted to make sure the string was long enough to contain any comment, even unrealistically long ones. Thanks! – user3101076 Dec 13 '13 at 23:19
1

It seems you are attempting to pass the address of infile to fgets.

fgets is defined as:

char *fgets(char *s, int size, FILE *stream);

In your function parseInputStream infile is a stream.

void parseInputStream(FILE *file, FILE *infile){

Therefore you don't need to pass the reference to infile when you call fgets.

fgets(string, 9999999, &infile);

It should be:

fgets(string, 9999999, infile);
alvits
  • 6,550
  • 1
  • 28
  • 28
  • I was switching around all of the file pointers trying to fix the segmentation fault. This error was just me not being thorough when I was putting my code back to the way it originally was submitted my question. – user3101076 Dec 13 '13 at 23:27