?
Solved

SIGSEGV, Segmentation fault in fprintf()

Posted on 2003-03-18
9
Medium Priority
?
2,236 Views
Last Modified: 2008-02-01
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
Comment
Question by:queryanalyzer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 8161759
can you post the abort_program function and/or more of the code?

Marc
0
 
LVL 3

Expert Comment

by:marcjb
ID: 8161798
I ask because it is possible that if the abort_program function is not exiting, you would be calling fprintf with NULL.

Marc
0
 

Author Comment

by:queryanalyzer
ID: 8161954
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:marcjb
ID: 8162088
>>>  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
 
LVL 6

Expert Comment

by:zebada
ID: 8162202
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
 
LVL 5

Expert Comment

by:ecw
ID: 8163612
Don't use stdio in signal handlers.
0
 
LVL 6

Expert Comment

by:zebada
ID: 8163707
Ok, ignore what I said - I misread the stack trace.
0
 
LVL 11

Accepted Solution

by:
cup earned 225 total points
ID: 8164545
Looks like you have a heap problem before calling record.  Check your boundaries, memory allocations etc.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8164837
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question