Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

main

gcc

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()?

like image 442
Deqing Avatar asked Jan 26 '14 05:01

Deqing


People also ask

Should you put functions before or after main?

For a function defined in the same file where main is defined: If you define it before main , you only have to define it; you don't have to declare it and separately define it. If you define it after main , you have to put a matching prototype declaration before main .

Do functions go before or after main in C?

Functions that are executed before and after main() in C With GCC family of C compilers, we can mark some functions to execute before and after main(). So some startup code can be executed before main() starts, and some cleanup code can be executed after main() ends.

Is it possible to run without main function?

Yes, we can execute a java program without a main method by using a static block.

Why do we need to declare a function before the main program?

Because if you have defined the function below main() and not declared the prototype above the main() then while the program is executing then it starts from main() and it will completely ignore the function because it wouldn't know that it exists.


3 Answers

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

like image 158
harmic Avatar answered Oct 27 '22 20:10

harmic


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.

like image 23
mockinterface Avatar answered Oct 27 '22 21:10

mockinterface


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.

like image 20
JuanR Avatar answered Oct 27 '22 19:10

JuanR