473,749 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=fop en("instruction ","a++");
fwrite(add,size of(add),1,instr uction);
fscanf(instruct ion,"%s",&show) ;
printf("%s",sho w);
fcloseall();
}

Jan 10 '06 #1
12 2307
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(size of(a));
show=malloc(siz eof(s));

Jan 10 '06 #4
"nae zot bba la" <zo*****@optusn et.com.au> wrote in message
news:pa******** *************** *****@optusnet. com.au...
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=fop en("instruction ","a++");
Undefined behavior.
fwrite(add,size of(add),1,instr uction);
Undefined behavior.
fscanf(instruct ion,"%s",&show) ;
Undefined behavior.
printf("%s",sho w);
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(size of(a));
show=malloc(siz eof(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*****@optusn et.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)cybers pace.org | don't, I need to know. Flames welcome.
Jan 11 '06 #8
Christopher Benson-Manica <at***@ukato.fr eeshell.org> wrote:
nae zot bba la <zo*****@optusn et.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.fr eeshell.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_Keit h) 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

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

Similar topics

6
8425
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 line and it threw a Segmentation Fault. Googling produces a ton of information on this subject but al of specific to certain binaries and it doesn't seem germaine to solving my problem. Any one have any advice? Whole script is below.
10
4487
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 namespace std;
2
5545
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 pointed to and data size/len to the file, after the structure. But when I tried to read the file in same sequence, it gives me segmentation fault. When I debugged the program with GDB, it executed well until fclose() and read all the member of...
16
8994
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 who don't speak french arbre means tree.
11
4731
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 obtain a new (longer) string. Example: string: languag letter: e
5
2996
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
27
3361
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
7
2088
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 graphviz binary. This works perfectly in a console but fails with a segfault when called under Apache/mod_cgi/perl regardsless of suexec beeing active or not. All Apache limits are off,
8
14697
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 include all of it- it's over 1k lines long). In addition, I'm including an "include" file where structures like "stack" are defined. Again, it's really long. I doubt the problem lies there, though, because the include file is used in many other...
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6801
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.