473,385 Members | 1,720 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,385 software developers and data experts.

Please help me overcome the segmentation fault

Rav
I have recently started working on GCC on red Hat 9. I have encountered
with some problems that i think should not occur (at least on Turbo C),
here they r:

Why does the following piece of code displays segmentation fault error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}

another problem is that, while using fgets i am bound to use getchar to
remove the unread '\n'character in stdin as fflush(stdin) doesn't work.

char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used
fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution. why isn't fflush(stdin) working, is there any
alternative of fflush(stdin).

plz help...thanks in advance

Oct 22 '06 #1
10 2480
The code looks fine as far as it goes. What comes after?

Oct 22 '06 #2

Rav wrote:
char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used
fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution. why isn't fflush(stdin) working, is there any
alternative of fflush(stdin).

plz help...thanks in advance
using fflush() on a terminal is not a great idea.

If there is an extra "\n", it's likely you had a previous scanf() which
did not absorb the trailing "\n". Either use the getchar() or have the
previous read or scanf() also read the "\n".

Oct 22 '06 #3
Rav wrote:
I have recently started working on GCC on red Hat 9. I have encountered
with some problems that i think should not occur (at least on Turbo C),
here they r:

Why does the following piece of code displays segmentation fault error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}
I can see nothing inherently wrong with this fragment,
so I suspect the problem may be in the code I cannot see.
Please post a minimal, complete, compilable example that
demonstrates your problem.
another problem is that, while using fgets i am bound to use getchar to
remove the unread '\n'character in stdin as fflush(stdin) doesn't work.
Normally, fgets() reads and consumes the input stream
until it has read and consumed exactly one '\n', the one
that terminates the line. (Exceptions: fgets() will stop
early, without reading a '\n', if it encounters end-of-file
or an input error or if the line is too long for the buffer.)
I'm not sure what you mean by "the unread '\n' character,"
since there usually won't be one.
char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used
fflush(stdin) was *never* supposed to be used. What
are you trying to accomplish?
fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution.
You say "it works fine," and you ask for a "solution," but you
haven't told us what problem you are trying to solve.
why isn't fflush(stdin) working, is there any
alternative of fflush(stdin).
fflush() performs an output operation. stdin is not an output
stream. That's why fflush(stdin) doesn't "work," just as

fputs("\n!dlrow ,olleH", stdin);

doesn't "work."

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 22 '06 #4
On 22 Oct 2006 07:26:31 -0700, "Rav" <ra*********@gmail.comwrote:
>I have recently started working on GCC on red Hat 9. I have encountered
with some problems that i think should not occur (at least on Turbo C),
here they r:

Why does the following piece of code displays segmentation fault error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
Not likely. This statement assigns p the address of list[0] so p
points to the 10. Given that you did not post a compilable example,
under the normal assumptions this code should work fine.
>q = list+4;
Ditto. q points to the 50.
>}

another problem is that, while using fgets i am bound to use getchar to
remove the unread '\n'character in stdin as fflush(stdin) doesn't work.
fgets will read the first \n it encounters in the stream unless it has
already transferred count-1 characters. Please try to describe the
problem in detail. fflush is defined only for output streams.
>
char temp[20];
printf("\n Enter string: ");
getchar(); //fflush(stdin) was supposed to be used
This removes the first character of input so you can never see it.
What makes you think it is a \n. The only time I have seen what you
describe actually happen is if this code is preceded by a getchar that
left a \n waiting in the stream.
>fgets(temp,20,stdin);

it works fine this way but i think is not a proper approach. plz
suggest a solution. why isn't fflush(stdin) working, is there any
alternative of fflush(stdin).

plz help...thanks in advance
Please post some compilable code and explain clearly what is happening
that you don't like.
Remove del for email
Oct 23 '06 #5
Rav wrote:
I have recently started working on GCC on red Hat 9. I have encountered
with some problems that i think should not occur (at least on Turbo C),
here they r:

Why does the following piece of code displays segmentation fault error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}
I'm temped to say that debugger is undefined, but that would have given
a compiler error and I suspect that your news editor wrapped the comment
line -- one reason to avoid // comments in Usenet.

As others have said, other than the wrapped comment, it looks OK.

--
Thad
Oct 23 '06 #6
Thad Smith wrote:
Rav wrote:
>I have recently started working on GCC on red Hat 9. I have
encountered with some problems that i think should not occur (at
least on Turbo C), here they r:

Why does the following piece of code displays segmentation fault
error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}

I'm temped to say that debugger is undefined, but that would have
given a compiler error and I suspect that your news editor wrapped
the comment line -- one reason to avoid // comments in Usenet.

As others have said, other than the wrapped comment, it looks OK.
No, list is an array of ints, not a pointer. The statement should
have read:

p = &list[0] + 4;

If that had happened after passing list to an external routine, the
statement would have been correct in that routine because of the
automatic conversion to a pointer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 23 '06 #7
CBFalconer said:
Thad Smith wrote:
>Rav wrote:
<snip>
>>>
Why does the following piece of code displays segmentation fault
error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}
<snip>
>>
As others have said, other than the wrapped comment, it looks OK.

No, list is an array of ints, not a pointer. The statement should
have read:

p = &list[0] + 4;
You lost me. Firstly, did you mean q, rather than p? If not, your code
doesn't have the same semantics as p = list, and if you did, how is:

q = &list[0] + 4;

semantically different from:

q = list + 4;

?

--
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)
Oct 23 '06 #8
CBFalconer wrote:
Thad Smith wrote:
>Rav wrote:
>>I have recently started working on GCC on red Hat 9. I have
encountered with some problems that i think should not occur (at
least on Turbo C), here they r:

Why does the following piece of code displays segmentation fault
error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}
I'm temped to say that debugger is undefined, but that would have
given a compiler error and I suspect that your news editor wrapped
the comment line -- one reason to avoid // comments in Usenet.

As others have said, other than the wrapped comment, it looks OK.

No, list is an array of ints, not a pointer. The statement should
have read:

p = &list[0] + 4;
That's an issue of style. Those who prefer adding unnecessary & and []
around array names would probably change it directly to &list[4].
If that had happened after passing list to an external routine, the
statement would have been correct in that routine because of the
automatic conversion to a pointer.
Is this the real Chuck Falconer? I would not have expected him to make
such a fundamental mistake. You can always use the name of an array as a
pointer to its first element. Not just after passing it to another
function. There is nothing wrong with the code that the OP posted.

p = list; /* is equivalent to */ p = &list[0];

q = list + 4; /* is equivalent to */ q = &list[0] + 4;
/* or */ q = &list[4];

--
Simon.
Oct 23 '06 #9
Richard Heathfield wrote:
CBFalconer said:
>Thad Smith wrote:
>>Rav wrote:
<snip>
>>>>
Why does the following piece of code displays segmentation fault
error:

{
int list[]={10,20,30,40,50};
int *p, *q;
p = list; //the problem is somewhere here, i checked with the
debugger
q = list+4;
}
<snip>
>>>
As others have said, other than the wrapped comment, it looks OK.

No, list is an array of ints, not a pointer. The statement should
have read:

p = &list[0] + 4;

You lost me. Firstly, did you mean q, rather than p? If not, your code
doesn't have the same semantics as p = list, and if you did, how is:
You are right. It should have been "p = &list[0];"

Are you always roaming Usenet in the middle of the night? I have
an excuse, I have medical problems that get me up every hour or
two. But you seem to be a relatively young thing.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Oct 23 '06 #10
CBFalconer said:
Richard Heathfield wrote:
>CBFalconer said:
<snip>
>>>
No, list is an array of ints, not a pointer. The statement should
have read:

p = &list[0] + 4;

You lost me. Firstly, did you mean q, rather than p? If not, your code
doesn't have the same semantics as p = list, and if you did, how is:

You are right. It should have been "p = &list[0];"
Or, equivalently, just: p = list; which is what it actually was.
Are you always roaming Usenet in the middle of the night?
I don't consider 05:55GMT (06:55BST) to be the middle of the night. :-)

--
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)
Oct 23 '06 #11

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

Similar topics

2
by: sivignon | last post by:
Hi, I'm writing a php script which deals with 3 ORACLE databases. This script is launch by a script shell on an linux machine like this : /../php/bin/php ./MySript.php (PHP 4.3.3) My script...
3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
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: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
1
by: jwlkr | last post by:
Hi, I am trying to sort a vector of a user defined type: a class which represents points in cartesian coordinates. The vector of points needs to be sorted according to the value of the...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.