Segfault in an if

E

Emmanuel Delahaye

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.
 
H

H.A. Sujith

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;
//..
 
G

George Huber

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
 
F

Felipe Magno de Almeida

H.A. Sujith said:
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
(e-mail address 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)
 
M

Michael

H.A. Sujith said:
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.
 
C

Chris Torek

H.A. Sujith said:
news:<[email protected]>... [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;

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.
 
M

Mark F. Haigh

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 address removed)
 
K

Keith Thompson

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().
 
K

Kenny McCormack

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 [...][/QUOTE]

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.
 
M

Mark F. Haigh

Kenny said:
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?[/QUOTE]

I don't want any readers thinking that there's some kind of difference
between using exit and returning a value from main. People posting
answers here should take care to provide good advice, for the benefit of
less experienced readers. Changing the return to an exit is not good
advice in this context, and is not likely to solve or help solve any
problems.
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.

I think that's a bit heavy-handed. As you know, the consensus is that
people with problems post the smallest strict ANSI C fragment that can
reproduce the problem. If they can't post a problematic strict ANSI C
fragment, then they are generally redirected to the newsgroup for their
target platform.

Discussions of and questions about portable ANSI C code should not be
discouraged here. Rather, what should be discouraged (and actively is
my many regulars) is misleading or incorrect answers to these questions.


Mark F. Haigh
(e-mail address removed)
 
H

H.A. Sujith

H.A. Sujith said:
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;

Thats the problem! I forgot to put the braces.
(I'm kicking myself in the head with my leg right now, which is very
difficult BTW). But I still dont understand why my debugger(gdb 5.2)
said the problem was at line 8, the first 'if'. May be some compiler
(gcc 3.2) optimization I didn't consider.

Thanks for the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top