Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segfault in an if

Reply
Thread Tools

Segfault in an if

 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-17-2004
H.A. Sujith vient de nous annoncer :
> The following code is causing a segfault at the first if statement.
> Am I doing something wrong or is it a compiler bug?
>
> //----------
> #include <stdio.h>
>
> int main(int argc, char *argv[])
> {
> int c;
> FILE *inp;
>
> if(argc > 2) {


Do you mean here ?

> printf("Too many arguments\n");
> return 1;
> } else if(argc > 1)
> if((inp = fopen(argv[1], "r")) == NULL) {
> printf("error: unable to open file %s\n", argv[1]);
> return 2;
> }
> else
> inp = stdin;
> //..


Sounds good to me. Might be something else (try to rebuild all) or a
compiler's bug. Try with another one.


 
Reply With Quote
 
 
 
 
H.A. Sujith
Guest
Posts: n/a
 
      07-17-2004
The following code is causing a segfault at the first if statement.
Am I doing something wrong or is it a compiler bug?

//----------
#include <stdio.h>

int main(int argc, char *argv[])
{
int c;
FILE *inp;

if(argc > 2) {
printf("Too many arguments\n");
return 1;
} else if(argc > 1)
if((inp = fopen(argv[1], "r")) == NULL) {
printf("error: unable to open file %s\n", argv[1]);
return 2;
}
else
inp = stdin;
//..

--
mail to: randomzoo <at> spymac <dot> com
 
Reply With Quote
 
 
 
 
George Huber
Guest
Posts: n/a
 
      07-17-2004
On Sat, 17 Jul 2004 10:16:33 -0700, H.A. Sujith wrote:

> The following code is causing a segfault at the first if statement.
> Am I doing something wrong or is it a compiler bug?
>
> //----------
> #include <stdio.h>
>
> int main(int argc, char *argv[])
> {
> int c;
> FILE *inp;
>
> if(argc > 2) {
> printf("Too many arguments\n");
> return 1;
> } else if(argc > 1)
> if((inp = fopen(argv[1], "r")) == NULL) {
> printf("error: unable to open file %s\n", argv[1]);
> return 2;
> }
> else
> inp = stdin;
> //..


The following compiles and runs on my system (fedora core 2, gcc 3.3.3).
I would suspect that you have not found a compiler bug - printf is far
to common for behavior like this not to have been caught before.

int main(int argc, char** argv)
{
if(argc > 2)
{
printf("To many arguments\n");
return 1;
}
else
{
if(2 == argc)
{
printf("opening a file\n");
}
else
{
printf("redirecting stdin\n");
}
}

return 0;
}

Try doing a full recompilation, including manually removing any object
files, libraries and the like. If you still get an error, try running the
program under a debugger to see where you are actually seg-faulting.

Finally, as a side note, I use exit() to return from main not return. I
can not see this making any difference at all, but it is something that
you could try.

Hope this helps, if not try to provide more information - stack trace and
the like.

George


 
Reply With Quote
 
Felipe Magno de Almeida
Guest
Posts: n/a
 
      07-17-2004
H.A. Sujith wrote:

> The following code is causing a segfault at the first if statement.
> Am I doing something wrong or is it a compiler bug?
>
> //----------
> #include <stdio.h>
>
> int main(int argc, char *argv[])
> {
> int c;
> FILE *inp;
>
> if(argc > 2) {
> printf("Too many arguments\n");
> return 1;
> } else if(argc > 1)
> if((inp = fopen(argv[1], "r")) == NULL) {
> printf("error: unable to open file %s\n", argv[1]);
> return 2;
> }

HERE is the error!
this else behind is all about the if just above it.
not the else if, so when argc == 1, inp isnt initialized
the correction would be:
else if(argc > 1) {
if(...) {
}
}
else
inp = stdin;
> else
> inp = stdin;
> //..
>




--
Felipe Magno de Almeida
Ciencia da Computacao - Unicamp
http://www.velocityreviews.com/forums/(E-Mail Removed) - UIN: 146989862
Cause Rock and Roll can never die.
"if you want to learn something really well, teach it to a computer."
What is Communism?
Answer: "Communism is the doctrine of the conditions of the liberation
of the proletariat." (by Karl Marx)
 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      07-17-2004
"H.A. Sujith" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)> ...
> The following code is causing a segfault at the first if statement.
> Am I doing something wrong or is it a compiler bug?
>
> //----------
> #include <stdio.h>
>
> int main(int argc, char *argv[])
> {
> int c;
> FILE *inp;
>
> if(argc > 2) {
> printf("Too many arguments\n");
> return 1;
> } else if(argc > 1)
> if((inp = fopen(argv[1], "r")) == NULL) {
> printf("error: unable to open file %s\n", argv[1]);
> return 2;
> }
> else
> inp = stdin;
> //..


you sure it isn't the inp=stdin. That is not allowable you have to
close the file then fopen it again. I know it isn't the first or the
second one, both are correct.
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      07-17-2004
>"H.A. Sujith" <(E-Mail Removed)> wrote in message
>news:<(E-Mail Removed) >...

[snippage]

>> ... else if(argc > 1)
>> if((inp = fopen(argv[1], "r")) == NULL) {
>> printf("error: unable to open file %s\n", argv[1]);
>> return 2;
>> }
>> else
>> inp = stdin;


In article <(E-Mail Removed) >
Michael <(E-Mail Removed)> writes:
>you sure it isn't the inp=stdin. That is not allowable you have to
>close the file then fopen it again.


This claim is incorrect: "inp = stdin" is allowed, and sets the
"FILE *" object named "inp" to point to the same "FILE" object that
the name "stdin" refers to. (The "stdin" name is required to be
a macro, and might be defined as something like __stdin or &__sfiles[0]
or some such, but whatever the macro expands to must also have type
"FILE *" and point to a "FILE" object that ultimately refers to
the standard input.)

There is, however, an obvious bug in the code fragment remaining in
">>" above:

if (a)
if (b)
c();
else
d();

is improperly indented, and does not mean what it seems to suggest at
first glance. Fixing the indenting results in:

if (a)
if (b)
c();
else
d();

In other words, the "inp = stdin" assignment occurs only if argc > 1
*and* the fopen() call succeeds.

If the fopen() call does succeed, the reference to the underlying
FILE object is lost when "inp = stdin" overwrites the non-NULL "inp".
If argc <= 1, the variable "inp" remains uninitialized.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Mark F. Haigh
Guest
Posts: n/a
 
      07-18-2004
George Huber <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) >...
> On Sat, 17 Jul 2004 10:16:33 -0700, H.A. Sujith wrote:


<snip>

>
> Finally, as a side note, I use exit() to return from main not return. I
> can not see this making any difference at all, but it is something that
> you could try.



You were doing fine until you said this. Please do not mislead the
youth. Quoth the standard:


5.1.2.2.3 Program termination

1. If the return type of the main function is a type compatible with
int, a return from the initial call to the main function is equivalent
to calling the exit function with the value returned by the main
function as its argument [...]


Mark F. Haigh
(E-Mail Removed)
 
Reply With Quote
 
Anthony Roberts
Guest
Posts: n/a
 
      07-19-2004
> HERE is the error!
> this else behind is all about the if just above it.


eep

The lesson here is: Use emacs because it makes these problems obvious.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-19-2004
(E-Mail Removed) (Mark F. Haigh) writes:
> George Huber <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed) >...
> > On Sat, 17 Jul 2004 10:16:33 -0700, H.A. Sujith wrote:

>
> <snip>
>
> >
> > Finally, as a side note, I use exit() to return from main not return. I
> > can not see this making any difference at all, but it is something that
> > you could try.

>
>
> You were doing fine until you said this. Please do not mislead the
> youth. Quoth the standard:
>
>
> 5.1.2.2.3 Program termination
>
> 1. If the return type of the main function is a type compatible with
> int, a return from the initial call to the main function is equivalent
> to calling the exit function with the value returned by the main
> function as its argument [...]


But I think the equivalence breaks down in some obscure circumstances
involving atexit().

--
Keith Thompson (The_Other_Keith) (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
 
Kenny McCormack
Guest
Posts: n/a
 
      07-19-2004
In article <(E-Mail Removed) >,
Mark F. Haigh <(E-Mail Removed)> wrote:
....
>> Finally, as a side note, I use exit() to return from main not return. I
>> can not see this making any difference at all, but it is something that
>> you could try.

>
>
>You were doing fine until you said this. Please do not mislead the
>youth. Quoth the standard:
>
>
>5.1.2.2.3 Program termination
>
>1. If the return type of the main function is a type compatible with
>int, a return from the initial call to the main function is equivalent
>to calling the exit function with the value returned by the main
>function as its argument [...]


I'm sorry. Your point was?

If your point is: In a conforming implementation, there shouldn't be any
difference between them (and thus to imply that you shouldn't even consider
trying to solve your real world problem by trying one when you find that
the other doesn't do what you want), then, well, you're entitled, but you
might want to read the next paragraph.

Perhaps we need to make clear to all prospective posters here (by, at
a start, putting it in the FAQ) that helping people to solve their real
world problems is off-topic. Note: I am not saying for a second that there
is anything wrong with this stance - in fact, I rather like it, for
a variety of reasons, not least of which that it tends to keep the quality
level of the discussion high.

 
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
invoking a segfault within a segfault handler - is this undefinedbehavior? Andrey Vul C Programming 8 07-30-2010 02:14 PM
Adding empty copy constructor makes segfault go away? William Payne C++ 4 08-28-2004 06:09 AM
istream segfault Jim Strathmeyer C++ 4 07-23-2004 03:47 PM
multiset segfault Arthur J. O'Dwyer C++ 10 06-18-2004 03:21 AM
STL string segfault Vedran Vyroubal C++ 5 03-04-2004 07:56 AM



Advertisments