Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Segmentation fault (http://www.velocityreviews.com/forums/t443648-segmentation-fault.html)

Shuaib 07-22-2006 06:35 PM

Segmentation fault
 
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==========


Tom St Denis 07-22-2006 06:43 PM

Re: Segmentation fault
 
Shuaib wrote:
> #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


spibou@gmail.com 07-22-2006 06:57 PM

Re: Segmentation fault
 
Shuaib wrote:

> 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


Shuaib 07-22-2006 07:18 PM

Re: Segmentation fault
 
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.


Shuaib 07-22-2006 07:28 PM

Re: Segmentation fault
 

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?


Tom St Denis 07-22-2006 07:28 PM

Re: Segmentation fault
 

Shuaib wrote:
> 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


Kenny McCormack 07-22-2006 08:00 PM

Re: Segmentation fault
 
In article <1153596507.767593.91850@h48g2000cwc.googlegroups. com>,
Shuaib <aries.shuaib@gmail.com> wrote:
>
>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


Tom St Denis 07-22-2006 08:03 PM

Re: Segmentation fault
 
Kenny McCormack wrote:
> >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.


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 ****ing 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


Keith Thompson 07-22-2006 08:18 PM

Re: Segmentation fault
 
"Shuaib" <aries.shuaib@gmail.com> writes:
> 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.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Shuaib 07-22-2006 08:25 PM

Re: Segmentation fault
 

Phew!

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



All times are GMT. The time now is 04:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.