LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-09-2003, 10:44 AM   #1
paul76
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Rep: Reputation: 0
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.
 
Old 09-09-2003, 11:10 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
a knackered glibc or gcc possibly?

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

Last edited by kev82; 09-09-2003 at 01:14 PM.
 
Old 09-09-2003, 11:16 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,373

Rep: Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962Reputation: 1962
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.
 
Old 09-09-2003, 11:25 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.

Last edited by Hko; 09-09-2003 at 11:28 AM.
 
Old 09-09-2003, 11:31 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Just after writing my previous post, I read acid_kewpie's.
Seems obvious to me that he's right.
 
Old 09-09-2003, 11:31 AM   #6
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Also, you can strace it to see exactly what system call crashes it...
 
Old 09-09-2003, 02:18 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Or gdb of course.
 
Old 09-09-2003, 05:16 PM   #8
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,533

Rep: Reputation: 148Reputation: 148
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.
 
Old 09-10-2003, 07:15 AM   #9
paul76
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Original Poster
Rep: Reputation: 0
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.
 
Old 09-10-2003, 08:10 AM   #10
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
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.
 
Old 09-10-2003, 06:40 PM   #11
bastard23
Member
 
Registered: Mar 2003
Distribution: Debian
Posts: 275

Rep: Reputation: 30
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
 
Old 09-11-2003, 10:00 AM   #12
paul76
LQ Newbie
 
Registered: Sep 2003
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you.

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

Paul.
 
Old 09-11-2003, 12:17 PM   #13
jspenguin
Member
 
Registered: Feb 2003
Location: Wichita, KS
Distribution: Heavily modified Redhat
Posts: 194

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program crash data..? RoaCh Of DisCor Mandriva 2 03-20-2005 10:04 PM
This causes my program to crash/hang in C... Scrag Programming 1 12-18-2004 04:53 PM
xmms crash xine crash mplayer crash paledread Linux - Software 9 03-09-2004 07:09 AM
Icewm background program in .xinitrc causing X crash qwijibow Linux - General 4 10-21-2003 11:25 PM
Free a used port after program crash Dek Linux - Networking 10 05-01-2003 09:20 AM


All times are GMT -5. The time now is 06:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration