views:

277

answers:

3

Hi! 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 foo a;
    a.d=0;
    a.i=0;
    return a.i;
}

If I put

return a;

compiler doesn't crash.

+1  A: 

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.

Jerry Coffin
Bad code should never crash a compiler. If the program itself crashes because of undefined behavior is a different story.
Maister
Maybe if the compiler crashed more frequently on such heinous constructs, we wouldn't see such horrid code in the wild. +100 for crash the hell out of anyone trying to compile the above.
Nathan Ernst
I remember reading somewhere about a "hacker's" C compiler that would output nothing on success, but on *any* error would only output "No".
dreamlax
A: 

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)

yan bellavance
His point is that he's defining main to return an instance of a struct...
Jerry Coffin
+2  A: 

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.

pbernatchez
Here' how to reproduce the problem: Start visual studio 2008 SP1 and make a new empty C++ project. Add a new source file and add .c extension by hand. This way it will be compiled as C. Press F7.If you didn't change any settings, it will be build a debug build. Result: Microsoft optimizing compiler has stopped working. Event name: APPCRASH. Oh, and I read the question again. The evil and something like are informative enough. Their point of existence is to avoid comments like "C standard says int main (void) blah blah blah" because I already know that :) and also describes the use of the code
AndrejaKo
1 nad 2 are correct. I guess I can't express myself in a good way sometimes. Your answer us now actually useful, so +1 and thanks for the ideas.
AndrejaKo