Segfault中的if [英] Segfault in an if
问题描述
以下代码在第一个if语句处导致段错误。
我做错了什么或是编译器错误?
// ----------
#include< stdio.h>
int main(int argc,char * argv [])
{
int c;
FILE * inp;
if(argc> 2 ){
printf(太多参数\ n);
返回1;
}否则if(argc> 1) )
if((inp = fopen(argv [1]," r"))== NULL){
printf("错误:无法打开文件%s \ n",argv [1]);
返回2;
}
其他
inp = stdin;
// ..
-
邮寄到:randomzoo< at> spymac< dot> com
H.A。 Sujith vient de nous annoncer:以下代码在第一个if语句中引起了段错误。
我做错了什么或是编译器错误?
> // ----------
#include< stdio.h>
int main(int argc,char * argv [])
{
int c;
FILE * inp;
if(argc> 2){
你的意思是??br />
printf(" Too many arguments \\\
");
返回1;
}如果(argc> 1)
if((inp = fopen( argv [1],r,)== NULL){
printf(错误:无法打开文件%s \ n,argv [1]);
返回2;
}
其他
inp = stdin;
//..
听起来不错。可能是其他东西(尝试重建所有)或
编译器的错误。尝试另一个。
2004年7月17日星期六,下午10:16:33 -0700,H.A。 Sujith写道:
以下代码在第一个if语句中导致段错误。
我做错了什么或是编译器错误?
// ----------
#include< stdio.h>
int main(int argc,char * argv [])
{
int c;
FILE * inp;
if(argc> 2){
printf(" Too many arguments\\\
);
返回1;
}否则if(argc> 1)
if((inp = fopen(argv [1]," r"))== NULL){
printf(错误:无法打开文件%s \ n,argv [1]);
返回2;
}
其他
inp = stdin;
//..
以下编译并在我的系统上运行(fedora core 2,gcc 3.3.3)。
I会怀疑你没有找到编译器错误 - printf远远没有>
这样的行为很常见,以前没有被捕获过。
int main(int argc,char ** argv)
{
if(argc> 2)
{
printf(To many arguments \\\
);
返回1;
}
else
{
if(2 == argc)
{
printf(打开文件\ n);
}
其他
{
printf(重定向stdin \ n);
}
}
返回0;
}
尝试进行完全重新编译,包括手动删除任何对象
文件,库等。如果仍然出现错误,请尝试在调试器下运行
程序,看看你实际上是段错误的。
最后,作为旁注,我使用exit()从main返回不返回。我没有看到这个有什么不同,但是你可以试试这个是什么。
希望这个有帮助,如果没有尝试提供更多信息 - 堆栈跟踪和
之类的。
George
< BLOCKQUOTE> HA Sujith写道:
以下代码在第一个if语句中导致段错误。
我做错了什么或是编译器错误?
// ----------
#include< stdio.h>
int main(int argc,char * argv [])
{
int c;
FILE * inp;
if(argc> 2){
printf(" Too many arguments\\\
);
返回1;
}否则if(argc> 1)
if((inp = fopen(argv [1]," r"))== NULL){
printf(错误:无法打开文件%s \ n,argv [1]);
返回2;
}
这是错误!
其他背后的全部是关于if就在它之上。
不是其他的if,所以当argc == 1时,inp没有初始化
更正将是:
else if(argc> 1){
if(...){
}
}
其他
inp = stdin;否则
inp = stdin;
//..
-
Felipe Magno de Almeida >
Ciencia da Computacao - Unicamp
fe ******* *****@ic.unicamp.br - UIN:146989862
原因摇滚乐永远不会死。
如果你想学习真的很好,把它教给电脑。
什么是共产主义?
答案:共产主义是解放条件的学说
无产阶级。 (由卡尔·马克思撰写)
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
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.
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
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
fe************@ic.unicamp.br - 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)
这篇关于Segfault中的if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!