programming: main() isn’t so main() these days

by ,

In the C language, main() is the name of the function that gets called first when your program starts. Well, actually your global variables get initialized first, so if you say things like:

int fred = 43;

main()
{
	printf("fred is %d\n", fred);
}

You would get “43” printed out.

Now in C++, you can get objects declared before main(), as in:

BigObj jumbo;

main()
{
	printf("jumbo.foo() is %d\n", jumbo.foo());
}

So now the BigObj object “jumbo” has to be constructed before main() is entered, which means the method BigObj::BigObj() gets called and code gets run before main(). This has gotten to the point where you get hundreds or thousands of lines of code run before main() is called.

Today I discovered that Objective C has a similar setup. The class method +load gets called before main().
I mentioned this to Mark, and complained that maybe main() should be renamed to mostly_main() or perhaps main_except_for_a_few_fiddley_bits(). He came back and said: how about “Victoria Park” as it is right after Main.

I nearly fell off my chair laughing. Okay, not really, but I got a big stupid smile.

Yes this is a big long post explaining about code just so you could get a joke.
Thlppppt.

development.

About rae

I'm a long-time Mac and iOS developer. I'm also a big fan of Ruby on Rails and relational databases. I tend to work remotely, in my basement with occasional trips to the office. I'm also a big videophile, both TV and film, and can't wait for a good, inexpensive home 4k solution.

4 comments on “programming: main() isn’t so main() these days

  1. aep

    +load is rarely used. Most classes use +initialize which is called just before the first instance of the class is created.You can also use the NSApplication delegate method -applicationDidFinishLaunching to do any post launch setup. In the frameworks, we always try to limit any pre-main initialization since it affects launch time.

Leave a Reply

Your email address will not be published. Required fields are marked *