Code crashing compiler: main() returning a struct instead of an int
I'm experimenting with a piece of C code. Can anyone tell me why is VC 9.0 with SP1 crashing for me? Oh, and the code is meant to be an example used in a discussion why something like
void main (void)
is evil.
struct foo { int i; double d; } main (double argc, struct foo argv)
{
struct fo开发者_如何学编程o a;
a.d=0;
a.i=0;
return a.i;
}
If I put
return a;
compiler doesn't crash.
The code gives undefined behavior. This doesn't require the compiler to crash, it just says you've basically broken the rules, so the compiler hasn't done anything wrong, no matter what happens.
Edit: that said, as a quality of implementation issue, it's absolutely true that there's basically never an excuse for the compiler to crash -- reasonably speaking, it should always exit normally, no matter what data you feed it.
Ok you want to pose an esoteric question, then please construct a complete one.
How did you run the test? What do you mean by crash? What was your output? Did you just compile, or compile and link, or compile and link and debug? How is your compiler configured? Where are you going with this experiment?
phrases like: "something like" "evil" are not informative enough to be useful
Follow UP:
Instinctively I'll guess that this crash is a consequence of a compiler optimization switch with which you permit the the compiler to make certain assumptions that you are not conforming to.
my suppositions:
1- The void main(void) (without ;) is part of a comment you are making, but not part of the test you submitted.
2- Your program is incorrect, but this is deliberate in order to investigate
the behaviour of the compiler/linker/execution environment.
If this is indeed the case, you need to reduce the complexity of the test case.
Please simplify the test case to the bare minimum it takes to cause a crash. I can't do it for you, I don't have the correct versions of software installed, anyway, it's your experiment.
will this crash?
struct foo { int i; double d; };
struct foo main( void)
{
int a=0;
return a;
}
or even this most minimal example?
void * main(void)
{
return 0;
}
of is it this (I doubt it):
int main( double argc, char ** argv)
{
return 0;
}
You get the idea. reduce the crash to it's essence. Come up with a program that is error free except for the one thing that will make it crash.
Then report back.
put a semi colon between the end of your structure and main like so:
struct foo { int i; double d; }; main (double argc, struct foo argv)
you could also define a return type for main if your gonna return something:
struct foo { int i; double d; }; int main (double argc, struct foo argv)
精彩评论