473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Segmentation fault when compiled binary is executed

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();
}

Jan 10 '06 #1
12 2273
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.

Jan 10 '06 #2
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)
Jan 10 '06 #3
thx. so basically somethign like

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

Jan 10 '06 #4
"nae zot bba la" <zo*****@optusnet.com.au> wrote in message
news:pa****************************@optusnet.com.a 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
Jan 10 '06 #5
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)
Jan 10 '06 #6
> 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

Jan 10 '06 #7
nae zot bba la <zo*****@optusnet.com.au> 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.
Jan 11 '06 #8
Christopher Benson-Manica <at***@ukato.freeshell.org> wrote:
nae zot bba la <zo*****@optusnet.com.au> 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
Jan 11 '06 #9
Christopher Benson-Manica <at***@ukato.freeshell.org> 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) ks***@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.
Jan 11 '06 #10
Keith Thompson <ks***@mib.org> wrote:
I don't think I agree. Using exit() is fairly common.
At the end of main()? I accept that your experience is much broader
than mine, but FWIW I have seen many more return's than exit()'s at
the end of 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.


I suppose 1991 conventions may have been different; I was 11 at the
time, so I'm not qualified to comment. In any case, I admit that it
isn't much more than a question of style religion. Isn't there some
(negligible) overhead associated with the call to exit() as opposed to
a straight return, though?

Clearly, if God had intended exit() to be used instead of a return from
main(), He would have allowed void main( void ) as a valid prototype :-)

--
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.
Jan 12 '06 #11
>> "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.


I suppose 1991 conventions may have been different; I was 11 at the
time, so I'm not qualified to comment. In any case, I admit that it
isn't much more than a question of style religion. Isn't there some
(negligible) overhead associated with the call to exit() as opposed to
a straight return, though?


Since many implementations use startup code like:

_start() { exit(main(argc, argv)); }
(where getting the values of argc and argv is implementation-dependent).

isn't there some (negligible) overhead associated with the return
which doesn't happen if you call exit() directly?

Gordon L. Burditt
Jan 12 '06 #12
Gordon Burditt <go***********@burditt.org> wrote:
isn't there some (negligible) overhead associated with the return
which doesn't happen if you call exit() directly?
Sounds like six on one system, half a dozen on another. I was not
aware of
_start() { exit(main(argc, argv)); }


, so I appreciate the insight.

--
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.
Jan 12 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jerrygarciuh | last post by:
Hello, I have a script running in the wee hours via cron job. When I access the script via browser it works like a charm. I noticed it was not succeeding and ran it manually from the command...
10
by: Vishal Grover | last post by:
Hello Everyone, I am seeing a certain behaviour which I find strange, and am curious to get an explanation to it. I have the following program. #include <iostream> #include <cstdlib> using...
2
by: Atulvid | last post by:
Hi, I created a test file for my application by writing structures to a binary file. To make sure that I will get correct data for the pointers inside the structure, i wrote actual data they...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
11
by: Polar | last post by:
Hi! i'm a newbie in C language and i'm writing my first simple codes. In one of these, my purpose is to append the ascii value of an interger (example 101 --> e) at the end of a string to...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: Tim Evers | last post by:
Hi, though I'm some kind of experienced bug-hunter :) I have no idea what happens in the following case: linux system, apache w. suexec, perl, graphviz (2.8). A perl script calls the...
8
by: Bryan | last post by:
Hello all. I'm fairly new to c++. I've written several programs using std::vectors, and they've always worked just fine. Until today. The following is a snippet of my code (sorry, can't...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.