Http://www.cnblogs.com/Hacker/archive/2010/06/02/1750383.html
With this function, you can execute the operations you want before the main function is executed and after exiting the main function. The specific principle is that there are a lot of web pages, and you can find them by google. Here is just an example.
With this function, you can execute the operations you want before the main function is executed and after exiting the main function. The specific principle is that there are a lot of web pages, and you can find them by google. Here is just an example.
Running result:
Hello world!
Start = 0x4005fb
Stop = 0x40060b
Goodbye world!
Constructors and Destructors are special functions. these are one of the features provided by an Object Oriented Programming language. constructors and Destructors are defined inside an object class. when an object is instantiated, ie. defined of or dynamically allocated of that class type, the Constructor function of that class is executed automatically. there might be composed constructors of which the correct implementation is automatically selected by the compiler. when this object is destroyed or deallocated, the Destructor function is automatically executed. for example when the scope of the object has finished or the object was dynamically allocated and now being freed. the Constructors and the Destructors are generally contains initialization and cleanup codes respectively required by an object to operate correctly. because these functions are automatically invoked by the compiler therefore the programmer freed from the headache of calling them manually.
There is no such thing called 'configurator' and 'deletester' in C programming language or in structured languages, although there is no boundaries on defining such functions which act like them. you need to make functions which act like the constructors and destructors and then call them manually.
The GCC constructor and destructor attributes
GCC has attributes with which you can tell the compiler about how a lot of things shoshould be handled by the compiler. among such attributes the below function attributes are used to define constructors and destructors in C language. these wocould only work under GCC. as there is noObjectsAndClassApproach in C the working of these functions are not like C ++ or other OOP language constructor and destructors. With this feature, the functions defined as constructor function wocould be executed before the functionMainStarts to execute, and the destructor wocould be executed afterMainHas finished execution. The GCC function attributes to define constructors and destructors are as follows:
|
__attribute__((constructor))__attribute__((destructor))__attribute__((constructor (PRIORITY)))__attribute__((destructor (PRIORITY))) |
For example, to declare a function namedBegin ()As a constructor, andEnd ()As a destructor, you need to tellGccAbout these functions through the following declaration.
|
voidbegin (void) __attribute__((constructor));voidend (void) __attribute__((destructor)); |
An alternate way to flag a function as a C constructor or destructor can also be done at the time of the function definition.
|
__attribute__((constructor))voidbegin (void){ /* Function Body */}__attribute__((destructor))voidend (void){ /* Function Body */} |
After declaring the functions as constructors and destructors as abve,GccWill automatically callBegin ()Before callingMain ()And callEnd ()After leaving main or after the executionExit ()Function. The following sample code demonstrates the feature.
|
#include <stdio.h> voidbegin (void) __attribute__((constructor));voidend (void) __attribute__((destructor)); intmain (void){ printf("\nInside main ()");} voidbegin (void){ printf("\nIn begin ()");} voidend (void){ printf("\nIn end ()\n");} |
Execution of this code will come up with an output which clearly shows how the funere were executed.
123 |
In begin ()Inside main ()In end () |
Multiple Constructors and Destructors
Multiple constructors and destructors can be defined and can be automatically executed depending upon their priority. In this case the syntax is_ Attribute _ (constructor (PRIORITY )))And_ Attribute _ (destructor (PRIORITY ))). In this case the function prototypes wowould look like.
12345678 |
voidbegin_0 (void) __attribute__((constructor (101)));voidend_0 (void) __attribute__((destructor (101))); voidbegin_1 (void) __attribute__((constructor (102)));voidend_1 (void) __attribute__((destructor (102))); voidbegin_2 (void) __attribute__((constructor (103)));voidend_2 (void) __attribute__((destructor (103))); |
The constructorsLower priorityValue wocould be executed first. The destructorsHigher priorityValue wocould be executed first. So the constructors wocould be called in the sequence:Begin_0,Begin_1 (),Begin_2 (). And the destructors are called in the sequenceEnd_2 (),End_1 (),End_0 (). Note the LIFO execution sequence of the constructors and destructors depending on the priority values.
The sample code below demonstrates this
|
#include <stdio.h> voidbegin_0 (void) __attribute__((constructor (101)));voidend_0 (void) __attribute__((destructor (101))); voidbegin_1 (void) __attribute__((constructor (102)));voidend_1 (void) __attribute__((destructor (102))); voidbegin_2 (void) __attribute__((constructor (103)));voidend_2 (void) __attribute__((destructor (103))); intmain (void){ printf("\nInside main ()");} voidbegin_0 (void){ printf("\nIn begin_0 ()");} voidend_0 (void){ printf("\nIn end_0 ()");} voidbegin_1 (void){ printf("\nIn begin_1 ()");} voidend_1 (void){ printf("\nIn end_1 ()");} voidbegin_2 (void){ printf("\nIn begin_2 ()");} voidend_2 (void){ printf("\nIn end_2 ()");} |
The output is as below:
1234567 |
In begin_0 ()In begin_1 ()In begin_2 ()Inside main ()In end_2 ()In end_1 ()In end_0 () |
Note that, when compiling with priority values between 0 and 100 (inclusive ),GccWocould throw you warnings that the priority values from 0 to 100 are reserved for implementation, so these values might be used internally that we might not know. so it is better to use values out of this range. the value of the priority does not depend, instead the relative values of the priority is the determinant of the sequence of execution.
Note that the functionMain ()Is not the first function/code block to execute in your code there are a lot of code already executed beforeMainStarts to execute. The functionMainIs the user's code entry point, but the program entry point is notMainFunction. There is a startup function which prepares the environment for the execution. The startup functions first call the functions declared as constructors and then calltheMain. WhenMainReturns the control to the startup function it then callthose funich which you have declared as the destructors. There are separate sections in the executable. CtorsAnd. DtorsWhich hold these functions. (not discussed here ).
As there is no class object creation in C language does not have such features like in C ++ or other OOP programming ages but this feature can bring some flexibility by calling the functions automatically on execution and termination of the code which you needed to do inside the main. for example one use may be like, you have a dynamically allocated global variable, might point to a linked list head or an array, or a file descriptor which you can allocate inside a constructor. if some error is encountered you can immediately callExit ()Or the program terminates normally, depending on the error code you can make cleanup in the destructors.
Another good use is probably calling the initialization functions of some library, a bunch of which needs to be called in each program. you can make a separate file with the constructor files calling properly the library functions (probably operating on globals) and calling the library cleanup functions in the destructors. whenever you make a program what you need to do is to compile your code with this file containing the constructors and destructors and forget about calling the initialization and cleanup functions in the main program. but doing this will make your code unportable, as it wocould only work under GCC.