Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of operations might one need to do before main()

Tags:

c++

c

main

I came across this question asking how to execute code before main() in C, mentioning there were strategies for C++. I've mostly lived in application space, so executing before main() has never occurred to me. What kind of things require this technique?

like image 808
dabhaid Avatar asked Feb 02 '11 23:02

dabhaid


People also ask

What happens before main () in C?

For most C and C++ programs, the true entry point is not main , it's the _start function. This function initializes the program runtime and invokes the program's main function. The use of _start is merely a general convention. The entry function can vary depending on the system, compiler, and standard libraries.

Can we call a function before main function in C?

Functions that are executed before and after main() in CWhen the attribute is constructor attribute, then it will be executed before main(), and when the attribute is destructor type, then it will be executed after main().

Is it possible to execute code even after the program exits the main () function?

No, it is not possible. When the main method exists, the program terminates.


2 Answers

"What kind of things require this technique?"

Point of fact: none.

However, there are a lot of useful things you might WANT to do before main for a variety of reasons. For just one practical example, say you have an abstract factory that builds doohickies. You could make sure to build the factory instance, assign it to some special area, and then register the various concrete doohickies to it...yes, you can do that.

On the other hand, if you implement the factory as a singleton and use the facts of global value initialization to "trick" the implementation into registering concrete doohickies before main starts you gain several benefits with very few costs (the fact of using singletons, basically a non-issue here, is pretty much the only one).

For example you:

  1. Don't have to maintain a list of registrations that all must be explicitly called. In fact, you can even declare and define an entire class in private scope, out of sight of anyone, and have it available for use when the program starts.

  2. main() doesn't have to do a bunch of crap with a bunch of objects it doesn't care about.

So, none of this is actually necessary. However, you can reduce coupling and maintenance issues if you leverage the fact that globals are initialized before main begins.

Edit:

Should note here that I've since learned that this isn't guaranteed by the language. C++ only guarantees that zero or constant initialization happens before main. What I talk about in this answer is dynamic initialization. This C++ guarantees happens before the first use of the variable, much like function-local static variables.

Every compiler though seems to do dynamic initialization before main. I thought I ran into one once that did not but I believe the source of the issue was something else.

like image 171
Edward Strange Avatar answered Nov 08 '22 07:11

Edward Strange


This technique can be used for library initialization routines or for initializing data that will be used implicitly during the execution of the program.


GCC provides constructor and destructor function attributes that cause a function to be called automatically before execution enters main() or main() has completed or exit() has been called, respectively.

void __attribute__ ((constructor)) my_init(void);
void __attribute__ ((destructor)) my_fini(void);

In the case of library initialization, constructor routines are executed before dlopen() returns if the library is loaded at runtime or before main() is started if the library is loaded at load time. When used for library cleanup, destructor routines are executed before dlclose() returns if the library is loaded at runtime or after exit() or completion of main() if the library is loaded at load time.

like image 28
jschmier Avatar answered Nov 08 '22 06:11

jschmier