在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
http://www.cnblogs.com/Hacker/archive/2010/06/02/1750383.html 使用这个功能,你就可以在main函数执行之前,和main函数退出之后,执行你自己想要的操作。具体原理,网上很多,自己google一下就找到了,这里只是给一个例子。 使用这个功能,你就可以在main函数执行之前,和main函数退出之后,执行你自己想要的操作。具体原理,网上很多,自己google一下就找到了,这里只是给一个例子。
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 static void start(void) __attribute__ ((constructor)); 5 static void stop(void) __attribute__ ((destructor)); 6 7 int 8 main(int argc, char *argv[]) 9 { 10 printf("start == %p\n", start); 11 printf("stop == %p\n", stop); 12 13 exit(EXIT_SUCCESS); 14 } 15 16 void17 start(void) 18 { 19 printf("hello world!\n"); 20 } 21 22 void23 stop(void) 24 { 25 printf("goodbye world!\n"); 26 } 27
运行结果: 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 many 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 ‘constructors’ and ‘destructors’ 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 attributesGCC has attributes with which you can tell the compiler about how a lot of things should be handled by the compiler. Among such attributes the below function attributes are used to define constructors and destructors in C language. These would only work under GCC. As there is no objects and class approach 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 would be executed before the function main starts to execute, and the destructor would be executed after the main has finished execution. The GCC function attributes to define constructors and destructors are as follows:
For example, to declare a function named begin () as a constructor, and end () as a destructor, you need to tell gcc about these functions through the following declaration.
An alternate way to flag a function as a C constructor or destructor can also be done at the time of the function definition.
After declaring the functions as constructors and destructors as above, gcc will automatically call begin () before calling main () and call end () after leaving main or after the execution of exit () function. The following sample code demonstrates the feature.
Execution of this code will come up with an output which clearly shows how the functions were executed.
Multiple Constructors and DestructorsMultiple 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 would look like.
The constructors with lower priority value would be executed first. The destructors with higher priority value would be executed first. So the constructors would be called in the sequence: begin_0, begin_1 (), begin_2 () . and the destructors are called in the sequence end_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
The output is as below:
Note that, when compiling with priority values between 0 and 100 (inclusive), gcc would 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 function main () is not the first function/code block to execute in your code there are a lot of code already executed before main starts to execute. The function main is the user’s code entry point, but the program entry point is not the main function. There is a startup function which prepares the environment for the execution. The startup functions first call the functions declared as constructors and then calls the main. When main returns the control to the startup function it then calls those functions which you have declared as the destructors. There are separate sections in the executable .ctors and .dtors which 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 languages 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 call exit () 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 would only work under GCC. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论