int (&main)() = f;

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
Doesn't compile link for me:

$ xlC -o test test.cc
ld: 0711-317 ERROR: Undefined symbol: .main

Last edited on
try this instead:
 
int (*main)() = f;


Last edited on
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.)
Will compile and run and crash in GCC.
fails to link in MSVC2008.
@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
That doesn't fix the underlying problem.

(Compiles for GCC 4.6.2, and still crashes.)
@Duoas
That doesn't fix the underlying problem.


This fixes the linker message.:)
LOL, true. I still wanna know where LB got this from.
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
Why not both?


SUB Hello
  PRINT "Hello world!"
END SUB

Hello

:-]
this is like minecraft logic for C++: "no int main()? Fk logic!" ROFL
Assuming Fk is Femto-kilowatts, you really have Fk'd logic.
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...
Topic archived. No new replies allowed.