Derek Halden
Derek Halden

Reputation: 2333

Weird segfault after open file?

I'm getting a seg fault when I try and print fname. Can someone explain why this is happening to me? Is it that I'm not allowed to write to a file, close it, and then read from a file? argv[2] is defined. I've tried with multiple different allocations.

int main(int argc, char *argv[]){
    //Other stuff

    char *rfile=calloc(1, 2049);
    strcpy(rfile, argv[2]);
    FILE *wfile;
    wfile = fopen(argv[2], "w");
    SaveIndexToFile(index, wfile, alpha);  // This does a whole bunch of writing to a the file.
    fclose(wfile);

    //alpha is declared here, and works just fine.
    RemakeIndex(rfile, alpha);
    return 1;
}
int RemakeIndex(char *fname, WordList * alpha){
    printf("%s", fname);
    return 1;
}

Upvotes: 0

Views: 81

Answers (4)

Derek Halden
Derek Halden

Reputation: 2333

It ended up being that I was allocating way too much memory on the heap. I had a loop that was allocating strings of max_length of an unsigned int. Thank you for all of your comments and help!

Upvotes: 0

tribeca
tribeca

Reputation: 462

Prototyping the function is very important, the GCC compiler will assume an implicitly declared function (RemakeIndex in your code) has two arguments which are both int, which would make your code look like this:

 int RemakeIndex(int fname, int alpha) {
    printf("%s", (char *)fname);
    return 1;
 }

On a 64 bit machine and with GCC where pointers are 64 bits and ints are 32 bits then your arguments will be truncated to 32 bits which is likely to cause a segfault. The other answers have mentioned prototyping the function and if you are using a 64bit compiler I would suggest that this is your problem.

Upvotes: 0

alk
alk

Reputation: 70971

Is it that I'm not allowed to write to a file, close it, and then read from a file?

Yes, you are not allowed to read from a file [stream] after it had been closed.


Note on the (OP's) wording:

  • char * rfile is called a "pointer to char".
  • FILE * is called a "file-pointer" (or also just "pointer to FILE) or commonly (but formally wrong) just "file".

Also RemakeIndex() is called in main() without proper protoyping.

To fix this

  • either add a prototype before main():

    int RemakeIndex(char *, WordList *);
    
  • or move the whole implementation of RemakeIndex() before main().


Also the printf() calls' output might not show up immediately on the console, as stdout is line buffered.

To fix this

  • either print out a trailing new-line:

    printf("%s\n", fname);
    
  • or printf to stderr, which itself isn't line bufferd by default:

    fprintf(strerr, "%s\n", fname);
    
  • or flush stdout after having printed to it:

    printf("%s\n", fname);
    fflush(stdout);
    

Upvotes: 2

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

You are not checking the return value of fopen. If the fopen fails it can return NULL. If you are doing something with NULL that can undefined behavior. Place this line after opening the file.

if ( wfile == NULL ){
    perror("fopen");
    return;
} 

And check whether the argc count is three. If you are not giving arguments to the ./a.out then accessing the argv[2] can also lead to segmentation fault.

Upvotes: 4

Related Questions