• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2348
  • Last Modified:

SIGSEGV, Segmentation fault in fprintf()

Hi..
This code is throwing a "Segmentation fault" when i try to write into the file.


int temp;
FILE *rec_file = fopen("/home/neminath/cs533/assign3/.quizlog", "a");
if(rec_file == NULL){
     printf("\n Error trying to open 'log' file.");
     abort_program(errno);
}
temp = fprintf(rec_file,"%s","\n");


Here is the output from "gdb"

>>>
Program received signal SIGSEGV, Segmentation fault.
0xff2c5974 in _malloc_unlocked () from /usr/lib/libc.so.1
(gdb) where
#0  0xff2c5974 in _malloc_unlocked () from /usr/lib/libc.so.1
#1  0xff2c57e8 in malloc () from /usr/lib/libc.so.1
#2  0xff30a2c0 in _findbuf () from /usr/lib/libc.so.1
#3  0xff300180 in _doprnt () from /usr/lib/libc.so.1
#4  0xff30365c in fprintf () from /usr/lib/libc.so.1    <<<<<<<<<---------------------
#5  0x125b8 in record ()
#6  0x12360 in time_out ()
#7  <signal handler called>
#8  0xff319398 in _read () from /usr/lib/libc.so.1
#9  0xff30a1dc in _filbuf () from /usr/lib/libc.so.1
#10 0xff30c974 in fgets () from /usr/lib/libc.so.1
#11 0x11fc0 in ask_question ()
#12 0x10d7c in main ()

Any help would be appreciate.
0
queryanalyzer
Asked:
queryanalyzer
1 Solution
 
marcjbCommented:
can you post the abort_program function and/or more of the code?

Marc
0
 
marcjbCommented:
I ask because it is possible that if the abort_program function is not exiting, you would be calling fprintf with NULL.

Marc
0
 
queryanalyzerAuthor Commented:
More code - SIGSEGV, Segmentation fault in fprintf()

void record() {
     int temp;

     FILE *rec_file = fopen("/home/neminath/cs533/assign3/.quizlog", "a");
     if(rec_file == NULL) {
     printf("\n Error trying to open 'log' file.");
        abort_program(errno);
     }

     printf("\n Bfore 'fprintf' in 'record'");
     if (ferror(rec_file)) printf("\n ERROR");
     else printf("\n rec_file OK");
     pos = ftell(rec_file);
     printf("\n File Position: %d", pos);
        temp = fprintf(rec_file,"%s","\n");
     printf("\n USERNAME: %s",user_name);
     temp = fprintf(rec_file,"%s",user_name);
     temp = fprintf(rec_file,"%s"," ");
     temp = fprintf(rec_file,"%s",file_name);
     temp = fprintf(rec_file,"%s"," ");
     temp = fprintf(rec_file,"%d",num_ques);
     temp = fprintf(rec_file,"%s"," ");
     temp = fprintf(rec_file,"%d",percent);
     printf("\n After 'fprintf' in 'record'");
     fclose(rec_file);
}

abort_program(int) just takes an integer and printer error statements depending on the integer value.

I'm calling this record() method from main().
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
marcjbCommented:
>>>  abort_program(int) just takes an integer and printer error statements depending on the integer value.

That may be the problem.  If fopen fails, rec_file is NULL.  If you try to call fprintf when rec_file is NULL, you will get a SIGSEGV.  In the case of an error, the record function should return without doing fprintfs or, have abort_program really exit the program.

for example:

if(rec_file == NULL) {
  printf("\n Error trying to open 'log' file.");
  abort_program(errno);
  return;
}


Hope this helps,

Marc
0
 
zebadaCommented:
The bottom of your stack trace is this:

#10 0xff30c974 in fgets () from /usr/lib/libc.so.1
#11 0x11fc0 in ask_question ()
#12 0x10d7c in main ()


So you should be looking at the value of the arguments that you send to the fgets() function that is called from inside your ask_question() function.

I don't know why you think that the problem is with the code that you posted.

Regards
Paul
0
 
ecwCommented:
Don't use stdio in signal handlers.
0
 
zebadaCommented:
Ok, ignore what I said - I misread the stack trace.
0
 
cupCommented:
Looks like you have a heap problem before calling record.  Check your boundaries, memory allocations etc.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Segmentation fault often also occus when you try to reference a memory location which is now free or deallocated. For example, if you are returning a pointer from a function, and let's say that the pointer pointed to a local variable of the function, then after the function execution finishes, the memory allocated for all its local variables is freed and the address returned is now free, not allocated. So effectively, the pointer in the callling function which held the address returned by this called function, is now pointing to garbage.

Since you have not provided your entire code, it is not possible to point out where it is occuring, but can you go through your code and see if the above information helps you to correct your problem.

Mayank.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now