Segmentation fault

S

Shuaib

The following simple code gives an error of: "Segmentation fault" just
as I press Enter after entering a word. Any idea why this might be
occuring?

I am using GCC to compile the programe. Here is an output of $gcc
--version

gcc (GCC) 3.3.5-20050130 (Gentoo 3.3.5.20050130-r1,
ssp-3.3.5.20050130-1, pie-8.7.7.1)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

=========CODE=========

#include <stdio.h>
int main()
{
printf("Enter a word: ");
char word[20];
scanf("%s", word);
printf("%s", word);
FILE *file;
file=fopen("file1", "r+");
fclose(file);

return 0;
}

=========CODE==========
 
T

Tom St Denis

Shuaib said:
#include <stdio.h>
int main()
{
printf("Enter a word: ");
char word[20];
scanf("%s", word);
printf("%s", word);
FILE *file;
file=fopen("file1", "r+");
fclose(file);

return 0;
}

Do this

gcc -g3 myfile.c -o myfile
gdb myfile

Then type "run" and hit enter

Tom
 
S

spibou

Shuaib said:
The following simple code gives an error of: "Segmentation fault" just
as I press Enter after entering a word. Any idea why this might be
occuring?

I am using GCC to compile the programe. Here is an output of $gcc
--version

gcc (GCC) 3.3.5-20050130 (Gentoo 3.3.5.20050130-r1,
ssp-3.3.5.20050130-1, pie-8.7.7.1)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

=========CODE=========

#include <stdio.h>
int main()
{
printf("Enter a word: ");
char word[20];
scanf("%s", word);
printf("%s", word);
FILE *file;
file=fopen("file1", "r+");
fclose(file);

return 0;
}

It could be that the word you enter through the keyboard is
longer than 20 characters. Or that file1 does not exist. You
should modify your code so that it checks that fopen does not
return NULL.

Spiros Bousbouras
 
S

Shuaib

Thanks both of the above. I got the solution now.

GDB showed that there was something wrong with fclose(). Chaging the
opening mode from r+ to w+ in fopen() did the trick. I guess the file
didn't exist and we needed to creat it, which the w+ handles, not the
r+. :)

Thanks again.
 
S

Shuaib

BTW, the thing that really bugs me is that, it gives "Segmentation
fault" error just as I press enter after the word (which is less than
20 chars :) ). At least it should go on with printing the word, and
only when coming by the fclose() should it give the error. Isn't that
how it is suppose to work?
 
T

Tom St Denis

Shuaib said:
Thanks both of the above. I got the solution now.

GDB showed that there was something wrong with fclose(). Chaging the
opening mode from r+ to w+ in fopen() did the trick. I guess the file
didn't exist and we needed to creat it, which the w+ handles, not the
r+. :)

I'm glad you actually used gdb. Good tool to learn [hint: use it!!!]

Note your problem isn't that you used the wrong mode. Your problem is
you didn't check the return of fopen(). Just because you used "w+"
doesn't mean it will return a successfully opened file. You could have
a full disk, lack permissions, etc, etc.

The real solution is to always check the return [this goes for the
*alloc() series of functions too].

Tom
 
K

Kenny McCormack

BTW, the thing that really bugs me is that, it gives "Segmentation
fault" error just as I press enter after the word (which is less than
20 chars :) ). At least it should go on with printing the word, and
only when coming by the fclose() should it give the error. Isn't that
how it is suppose to work?

The real, Unix-specific answer is: stdout is line buffered.

I will leave it to the pedants to come up with the religiously-correct
clc answer.

Useful clc-related links:

http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/C_programming_language
 
T

Tom St Denis

Kenny said:
The real, Unix-specific answer is: stdout is line buffered.

I will leave it to the pedants to come up with the religiously-correct
clc answer.

I'm gonna give you some advice my momma gave me when I was a wee lad.

"If yee ain't got nothing proper to say, then shut yer pie hole."

Seriously, drop the attitude. Either answer the fucking question
properly, or shut the **** up.

And the answer IS because the file stream isn't guaranteed to be
flushed until you either close it or send a newline. Not exactly
rocket science.

Tom
 
K

Keith Thompson

Shuaib said:
BTW, the thing that really bugs me is that, it gives "Segmentation
fault" error just as I press enter after the word (which is less than
20 chars :) ). At least it should go on with printing the word, and
only when coming by the fclose() should it give the error. Isn't that
how it is suppose to work?

Please quote context. Google now makes this easy to do, but read
<http://cfaj.freeshell.org/google/> anyway; see also
<http://www.caliburn.nl/topposting.html>.

You printed a string without a trailing newline. The system probably
buffered the string and waited for a newline before printing it
(stdout is typically line-buffered). Since the program blew up before
it had a chance to finish, the buffered output was lost.

Changing this:
printf("%s", word);
to this:
printf("%s\n", word);
would probably cause the output to appear before the seg fault. If
you don't want the newline for some reason, you can also call
fflush(stdout).

One more thing: your program mixes declarations and statements. This
is allowed by C99, and as an extension by some pre-C99 compilers, but
it's not permitted by the C90 standard. If you want your code to be
portable, put all your declarations at the beginning of the block,
followed by all your statements.
 
S

Shuaib

Phew!

Good material Tom, and Keith. Thanks alot! I got an understanding of
the problem now.
 
K

Kenny McCormack

I will leave it to the pedants to come up with the religiously-correct
clc answer.
....
And the answer IS because the file stream isn't guaranteed to be
flushed until you either close it or send a newline.[/QUOTE]

Thank you for not disappointing me. I knew I could count on you.
 
J

jacob navia

Tom St Denis a écrit :
I'm gonna give you some advice my momma gave me when I was a wee lad.

"If yee ain't got nothing proper to say, then shut yer pie hole."

Ahhh good advice was that. GOOD ADVICE.

Why don't you remember it more often?
 
B

Barry Schwarz

On 22 Jul 2006 11:35:05 -0700, "Shuaib" <[email protected]>
wrote:

snip
#include <stdio.h>
int main()
{
printf("Enter a word: ");
char word[20];

Unless you have a C99 compiler, definitions should precede statements.
That way, people whose compilers do check will also be able to help
you.
scanf("%s", word);
printf("%s", word);
FILE *file;
file=fopen("file1", "r+");
fclose(file);

return 0;
}


Remove del for email
 
C

Christopher Benson-Manica

Shuaib said:
The following simple code gives an error of: "Segmentation fault" just
as I press Enter after entering a word. Any idea why this might be
occuring?

That's already been answered, but I'm surprised no one has yet made
this observation:
char word[20];
scanf("%s", word);

These lines are absolutely no better than using gets(), a function
which should never be used. Be safe and specify a maximum length:

scanf("%19s",word); /* Leave space for '\0' at end */

Better yet, avoid all the perils of scanf() and use fgets().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top