如何在C语言中使用constructor和destructor,gcc环境

使用这个功能,你就可以在main函数执行之前,虚拟主机,和main函数退出之后,执行你自己想要的操作。具体原理,香港服务器租用,网上很多,自己google一下就找到了,这里只是给一个例子。

使用这个功能,你就可以在main函数执行之前,香港服务器,和main函数退出之后,执行你自己想要的操作。具体原理,网上很多,自己google一下就找到了,这里只是给一个例子。

} 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 attributes

GCC 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:

1

2

3

4

__attribute__((constructor))

__attribute__((destructor))

__attribute__((constructor (PRIORITY)))

__attribute__((destructor (PRIORITY)))

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.

1

2

void begin (void) __attribute__((constructor));

void end (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.

1

2

3

4

5

6

7

8

__attribute__((constructor)) void begin (void)

{

/* Function Body */

}

__attribute__((destructor)) void end (void)

{

/* Function Body */

}

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#include <stdio.h>

void begin (void) __attribute__((constructor));

void end (void) __attribute__((destructor));

int main (void)

{

printf (“\nInside main ()”);

}

void begin (void)

{

printf (“\nIn begin ()”);

}

void end (void)

{

printf (“\nIn end ()\n”);

}

Execution of this code will come up with an output which clearly shows how the functions were executed.

1

2

3

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 would look like.

1

2

3

4

5

6

7

8

void begin_0 (void) __attribute__((constructor (101)));

void end_0 (void) __attribute__((destructor (101)));

void begin_1 (void) __attribute__((constructor (102)));

void end_1 (void) __attribute__((destructor (102)));

void begin_2 (void) __attribute__((constructor (103)));

void end_2 (void) __attribute__((destructor (103)));

享受每一刻的感觉,欣赏每一处的风景,这就是人生。

如何在C语言中使用constructor和destructor,gcc环境

相关文章:

你感兴趣的文章:

标签云: