Oct 3, 2012 at 4:47pm UTC
1 2 3 4 5 6
int f()
{
return 0;
}
int (&main)() = f;
It compiles fine, but it crashes. Why does it even compile?
Last edited on Oct 3, 2012 at 4:47pm UTC
Oct 3, 2012 at 4:58pm UTC
Doesn't
compile link for me:
$ xlC -o test test.cc
ld: 0711-317 ERROR: Undefined symbol: .main
Last edited on Oct 3, 2012 at 4:59pm UTC
Oct 3, 2012 at 5:08pm UTC
Last edited on Oct 3, 2012 at 5:09pm UTC
Oct 3, 2012 at 5:12pm UTC
LOL! How did you come across that one?
It is valid syntactically , but it is invalid because main () is a special function, which cannot be (portably) treated any differently than the standard way. Your compiler might just be too stupid to realize that compiling it produces gibberish object code (due, again, to main ()'s special requirements).
(Compiles for GCC 4.3. And crashes.)
Oct 3, 2012 at 5:15pm UTC
Will compile and run and crash in GCC.
fails to link in MSVC2008.
Oct 3, 2012 at 5:45pm UTC
@Cubbi
Doesn't compile link for me:
@guestgulkan
fails to link in MSVC2008.
Try the following:)
1 2 3 4 5 6 7 8 9 10 11
extern "C"
{
int f()
{
return 0;
}
int ( &main )() = f;
}
Last edited on Oct 3, 2012 at 5:47pm UTC
Oct 3, 2012 at 5:50pm UTC
That doesn't fix the underlying problem.
(Compiles for GCC 4.6.2, and still crashes.)
Oct 3, 2012 at 8:09pm UTC
LOL, true. I still wanna know where LB got this from.
Oct 3, 2012 at 9:21pm UTC
I thought of it myself randomly, where would I have got it from? XD
Also, language design choice debate: should main be a function or just the one and only section of code that gets executed?
Last edited on Oct 3, 2012 at 9:28pm UTC
Oct 3, 2012 at 11:17pm UTC
Why not both?
SUB Hello
PRINT "Hello world!"
END SUB
Hello
:-]
Oct 4, 2012 at 12:57am UTC
this is like minecraft logic for C++: "no int main()
? Fk logic!" ROFL
Oct 4, 2012 at 2:39am UTC
Assuming Fk is Femto-kilowatts, you really have Fk'd logic.
Oct 4, 2012 at 2:51am UTC
I know! I first thought he was talking about Forward Kinematics, but he can't be, since you can't start a transformation without the first vector...