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?
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.
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().
No, it is not possible. When the main method exists, the program terminates.
"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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With