Re: Simple fun g++ code..

Marek Habersack (grendel@vip.maestro.com.pl)
Sun, 22 Mar 1998 01:16:22 +0100 (CET)


On Fri, 20 Mar 1998, Vincent Cojot (Gosseyn SandPath) wrote:

> > execution time, the initialization code did the programme constructor.
> > It must have noticed that it had a zero address for the main() function
> > and skipped it. Finally it did the programme destructor.
>
> Agreed too but what puzzled me was the need to include "exit(0)" in the
> constructor (without it, the program would get "Illegal instruction") and
> the fact that the compiler wouldn't complain about "main" being pretty
> much anything.. Probably something that asm code would tell me. I guess I
> would have my answer by now if I could read machine code but I'm not Linus
> Thorvalds.. :) :)
That's easy. The class created by the compiler contains some data in the code
segment PRIOR to the first function (the constructor). This is usually a
virtual dispatch table, which is empty in this case, but still it contains
some "junk" - that's what triggers the illegal instruction. By issuing exit()
one prevents the startup code from calling the main function (or whatever is
after thus named label)

> > You could experiment by naming the object "imain". I would predict a
> > link error.
>
> Exactly. What I was wondering was more if that kind of construct (no main
> function, but a "main" object) was correct or if it was just a trick (my
> friend tried to compile it on VC++ and it wouldn't compile. It wanted a
> "main" function).
That's probably because VC++ marks labels explicitly as CODE or DATA.

----
"Hello John, have you seen The Standard four hours ago?
They fished a chick out of the Old Father...
Blond hair, blue eyes... She said she would be an actress or something...
Nobody knows what she was planning for, where she was going...
Funny thing she had smile on her face, she was smiling..."
"What a waste!"
-- Fish "Chelsea Monday"

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu