Deqing
Deqing

Reputation: 14632

When do I need a function to run before or after main()?

GCC supports construtors/destructor functions which support running function before or after main():

The constructor attribute causes the function to be called automatically before execution enters main(). Similarly, the destructor attribute causes the function to be called automatically after main() completes or exit() is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.

Here is an example from GeeksforGeeks.

When is the proper scenario of using this feature? Especially a function to be called before main(), what is the difference if we just place it in start of main()?

Upvotes: 6

Views: 227

Answers (4)

arunb2w
arunb2w

Reputation: 1196

Suppose you have a global structure and you want to initialize memory to the structure before starting your program, you can put it inside the constructor, since it calls before main().

Similarly, if you want to free any existing memory before the end of the program you can do so in the destructor.

Upvotes: 1

ichramm
ichramm

Reputation: 6642

From mine point of view, module constructor have their meaning when making shared modules.

Shared modules don't have an specific initialization routine (there is DllMain on Windows, but i has it´s limitations).

For example, Asterisk PBX abuses of constructors because is strongly based on modules, it injects a constructor on each module at compilation time. This constructor gets called on dlload() and tells asterisk core whether the module has been loaded properly or not, allowing it to call specific functions on the module.

Upvotes: 1

harmic
harmic

Reputation: 30587

Such constructor and destructor functions are mainly useful when writing libraries.

If you are writing a library which needs to be initialised, then you would have to provide an initialisation function. But how would you ensure that it is run before any other of your library's functions? The use of the library would have to remember to call it, which they could easily forget.

One way to get the initialisation done automatically is to mark the function as a constructor.

See also: How to initialize a shared library on Linux

Upvotes: 2

mockinterface
mockinterface

Reputation: 14860

For the majority of scenarios there will be no difference. Everything that you want to do with global variables, singletons, memory, etc, you could theoretically do in main() and with plain static initializers.

The main scenario where this is marginally applicable is cross platform projects, where you would like to keep most of your common code in main, however on some platforms, mainly embedded ones, you would like to duplicate what the other OSes are doing before main - setting up environment variables, wiring standard file descriptors (stdin/stdout/stderr) to custom descriptors on your system, allocate your own custom memory manager - e.g., allocate your own stack for running main(), and so on.

Upvotes: 1

Related Questions