LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How could a program crash between 2 printf() 's? (http://www.linuxquestions.org/questions/programming-9/how-could-a-program-crash-between-2-printf-s-90894/)

paul76 09-09-2003 11:44 AM

How could a program crash between 2 printf() 's?
 
I have a program that work in win and solaris. It could be compileted on Linux, but give "Segmentation fault" at
execution between 2 printf() 's.

The sequence of code is:

printf("\n7");
printf(" \n8");
/*free(xd);
free(y);
free(yd);
free(x);*/
printf("\n8");
fclose(f);
fclose(g);

printf("\n9");
printf(" Program terminated \n");


and the result is:

7
8
Segmentation fault

How could a programme crash between 2 printf() 's?


Paul.

kev82 09-09-2003 12:10 PM

a knackered glibc or gcc possibly?

<edit> acid's right, i didnt notice the \n was before 8.

acid_kewpie 09-09-2003 12:16 PM

well it's not is it? it's crashing on the fclose() call, but you're getting no further output as you have no newline character there to flush the output buffer.

Hko 09-09-2003 12:25 PM

One possible cause may be that you are running a version of the executable that was compiled without the comment in place.

Another, more likely, cause may be that the program actually crashes on the "close(f)" or "close(g)" calls. One would expect the program to print another line saying "8", because there is another printf("\n8") before the close() calls. But my guess is that this does not happen due to compiler optimization. It seems to me that two identical statements after eachother ( printf"\n8" skipping the comment lines of course) is an ideal opportunity for the compiler to do some optimization tricks. Optimization is a known source for confusion when debugging, or when something goes wrong.

Do you compile with gcc's "-O.." option?
If so, try without it.

Hko 09-09-2003 12:31 PM

Just after writing my previous post, I read acid_kewpie's.
Seems obvious to me that he's right.

Strike 09-09-2003 12:31 PM

Also, you can strace it to see exactly what system call crashes it...

Hko 09-09-2003 03:18 PM

Or gdb of course.

Mara 09-09-2003 06:16 PM

To explain acids's post. He's right - it crashes on fclose. When printfs are used (not only, in fact) everything you print on the screen is first kept in a buffer. It's dsplayed when '\n' or a similar thing happens. So
Code:

printf("\n7");
printf(" \n8");

printf("\n8\n");
fclose(f);
fclose(g);

will print 7, two 8 and then crash.

paul76 09-10-2003 08:15 AM

Thank you for your answers.

Whasn't the best exemple of code. I saw that I wrote 2 printf("\n8"). Sorry.

The problem is the same: "Segmentation fault" between 2 printf(); 's.
For the code:

printf("\n7");
printf(" \n8");

/*free(xd);
free(y);
free(yd);
free(x);*/

printf("\n9");
fclose(f);
fclose(g);
printf("\n10");

the result debuged with starce is:

write(1, "7 \n", 37) = 3
write(1, "8\n", 28) = 2
close(3) = 0
munmap(0x40015000, 4096) = 0
1munmap(0x40016000, 8192) = 0
--- SIGSEGV (Segmentation fault) ---
+++ killed by SIGSEGV +++

and if is debuged with gdb is:

7
8

Program received signal SIGSEGV, Segmentation fault.
0x42074bd0 in _int_free () from /lib/i686/libc.so.6

Thank you.


Please....any sugestion.

kev82 09-10-2003 09:10 AM

its still crashing on the fclose, replace the printf("")'s with fprintf(stderr, "") this isnt buffered so you'll see exactly where the problem is.

bastard23 09-10-2003 07:40 PM

paul76,

Use the backtrace (bt) command in gdb. It will show you which functions were called at the time of the segfault.

I suspect that fclose(g) is the one failing. Are you checking that the fopen worked for both f and g?

Good Luck,
chris

paul76 09-11-2003 11:00 AM

Thank you.

You have rigth, fclose(); was the problem.

Paul.

jspenguin 09-11-2003 01:17 PM

Or, fflush(stdout).
And, does the \n really have to be before the number?


All times are GMT -5. The time now is 09:22 PM.