Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segmentation fault when compiled binary is executed

Reply
Thread Tools

Segmentation fault when compiled binary is executed

 
 
nae zot bba la
Guest
Posts: n/a
 
      01-10-2006
Hi, very short code here. Basically I want to open a file called
instruction in a++ mode and then write data of %s type to it, then read
from the same file and print to screen. The code compiles but when I
execute the binary and enter some string to expect the program to print
them for me to screen, segmentation fault occurs. Any ideas anyone?

#include <stdio.h>
#include <stdlib.h>

char *add;
char *show;
FILE *instruction;

main()
{
scanf("%s",&add);
instruction=fopen("instruction","a++");
fwrite(add,sizeof(add),1,instruction);
fscanf(instruction,"%s",&show);
printf("%s",show);
fcloseall();
}

 
Reply With Quote
 
 
 
 
sundeyu@gmail.com
Guest
Posts: n/a
 
      01-10-2006
You should allocate memory for add and show first. BTW, you should
close the file and then open it with read mode to get the string
printed out.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-10-2006
nae zot bba la said:

> Hi, very short code here. Basically I want to open a file called
> instruction in a++ mode


There is no such mode. You may be thinking of "a+" or "a+b".

> and then write data of %s type to it,


But there is no such type. You may be thinking of "sequence of char
terminated by the first null character", which is also known as "string" -
and this is not a data type but a data format.

> then read
> from the same file and print to screen. The code compiles but when I
> execute the binary and enter some string to expect the program to print
> them for me to screen, segmentation fault occurs. Any ideas anyone?
>
> #include <stdio.h>
> #include <stdlib.h>
>
> char *add;
> char *show;
> FILE *instruction;
>
> main()


Aside: the preferred form of main(), when taking no arguments, is:

int main(void)

but this is not the cause of your seg fault.

> {
> scanf("%s",&add);


Quite apart from the hazards involved in using %s with scanf, and quite
apart from the fact that you appear to be ignoring an extremely useful
value returned by scanf, there is also the problem that scanf not only
requires char * rather than char ** as its argument but also requires that
pointer to be pointing to sufficient storage to receive the data in
question. This is almost certainly the cause of your seg fault. Well, it's
the cause of /this/ seg fault, anyway. Once you've fixed it, I fully expect
you to get others.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
nae zot bba la
Guest
Posts: n/a
 
      01-10-2006
thx. so basically somethign like

add=malloc(sizeof(a));
show=malloc(sizeof(s));

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-10-2006
nae zot bba la said:

> thx. so basically somethign like
>
> add=malloc(sizeof(a));
> show=malloc(sizeof(s));


Whilst the above changes will indeed stop you from getting a segmentation
fault, this is only because they will instead give you compilation errors.

Please find a good C book, one that explains about storage, and read it very
closely until you get the hang of - at least - the following facts:

1) Everything has to be Somewhere. Every single character you need to store
requires a byte of storage in which to be stored.
2) Types *matter*.
3) The malloc function can return NULL.


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-10-2006
"nae zot bba la" <(E-Mail Removed)> wrote in message
news(E-Mail Removed) u...
> Hi, very short code here. Basically I want to open a file called
> instruction in a++ mode and then write data of %s type to it, then read
> from the same file and print to screen. The code compiles but when I
> execute the binary and enter some string to expect the program to print
> them for me to screen, segmentation fault occurs. Any ideas anyone?
>
> #include <stdio.h>
> #include <stdlib.h>
>
> char *add;
> char *show;
> FILE *instruction;


There's no reason for these objects to be defined at file scope.

>
> main()


int main(void)

> {
> scanf("%s",&add);


Undefined behavior.

> instruction=fopen("instruction","a++");


Undefined behavior.

> fwrite(add,sizeof(add),1,instruction);


Undefined behavior.

> fscanf(instruction,"%s",&show);


Undefined behavior.

> printf("%s",show);


Undefined behavior.

> fcloseall();


No such function in standard C.

return 0;

> }


You don't have a C text, do you? (If you do, you need much more study. If
you
don't, get one. See the reviews at www.accu.org for suggestions).

-Mike


 
Reply With Quote
 
nae zot bba la
Guest
Posts: n/a
 
      01-10-2006
> You don't have a C text, do you? (If you do, you need much more study. If
> you
> don't, get one. See the reviews at www.accu.org for suggestions).
>
> -Mike

MKW, i will go and read this from chapter 1 to the end.
http://publications.gbdirect.co.uk/c...c_program.html

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-11-2006
nae zot bba la <(E-Mail Removed)> wrote:

> MKW, i will go and read this from chapter 1 to the end.
> http://publications.gbdirect.co.uk/c...c_program.html


That seems like a much better resource than a lot of the supposed
tutorials to be found online, but it still is inferior to the K&R2
text (which you should purchase if you have the means to do so).
I spotted a few instances (I imagine more exist) where a C guru
(I am not one) would have cause to quibble:

1) int main() isn't incorrect, but int main( void ) is generally
preferable.

2) Ending main() with a call to exit( EXIT_SUCCESS ) isn't incorrect
either, but it isn't great style. return EXIT_SUCCESS is much more
conventional.

3) The language in 5.4.1 fails to describe "FOO" as a string literal
and therefore leaves the distinction between them and "ordinary"
strings unclear.

4) "The rules for assignment of pointers show that there is no need to
use a cast on the return value from malloc, but it is often done in
practice."

This statement might have been accurate in 1991, but it really
isn't these days. Casting the return value of malloc() is
generally discouraged.

In short, you might read the C FAQ along with the resource you cited:

http://c-faq.com

and fill in some of the holes.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      01-11-2006
Christopher Benson-Manica <(E-Mail Removed)> wrote:

> nae zot bba la <(E-Mail Removed)> wrote:
>
> > MKW, i will go and read this from chapter 1 to the end.
> > http://publications.gbdirect.co.uk/c...c_program.html

>
> That seems like a much better resource than a lot of the supposed
> tutorials to be found online, but it still is inferior to the K&R2
> text (which you should purchase if you have the means to do so).
> I spotted a few instances (I imagine more exist) where a C guru
> (I am not one) would have cause to quibble:


> 4) "The rules for assignment of pointers show that there is no need to
> use a cast on the return value from malloc, but it is often done in
> practice."
>
> This statement might have been accurate in 1991, but it really
> isn't these days. Casting the return value of malloc() is
> generally discouraged.


Counter-quibble: yes, it's accurate. Casting malloc() _is_ often done.
Shouldn't be, but it is.

Richard
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-11-2006
Christopher Benson-Manica <(E-Mail Removed)> writes:
[...]
> 2) Ending main() with a call to exit( EXIT_SUCCESS ) isn't incorrect
> either, but it isn't great style. return EXIT_SUCCESS is much more
> conventional.


I don't think I agree. Using exit() is fairly common. I tend to use
0 rather than EXIT_SUCCESS myself, unless I'm also using EXIT_FAILURE.

For no particularly good reason, I tend to think of EXIT_SUCCESS and
EXIT_FAILURE as arguments to exit() rather than as values to be
return'ed from main().

"exit(n);" and "return n;" (in main()) are almost exactly equivalent
anyway, so I don't worry much about which one anyone chooses to use.
(The only difference is the lifetime of variables local to main()
referred to indirectly from an atexit()-registered function; any
program for which this matters is far too obscure anyway.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
Segmentation fault using Firefox 15.0.2 Keith Lee Firefox 3 04-29-2006 05:45 PM
g++ compiled C++ code called from gcc compiled C code Klaus Schneider C++ 1 12-02-2004 01:44 PM
Xerces XML Parser Segmentation fault with Java Pud XML 0 11-06-2003 05:07 PM
Intel Xeon + Linux + IBM sdk 1.3.1 - getting Segmentation fault Alex Hunsley Java 17 11-06-2003 12:12 AM



Advertisments