Parse the function and the function of the design before the main function _c language

Source: Internet
Author: User
A few days ago, a simple test framework was written for new employees, allowing them to write test cases and execute them conveniently. One of the problems encountered during this period was how to get them to add test cases to the code that did not affect the test framework? A single piece of C + + can solve this problem, but one of the difficulties is to register a single piece before main. C + + can be a constructor to achieve registration, c how to register?
Finally looked at the data, the original can be defined before main call the function! With this feature, the modular design of C can be improved.
Feature Description:
If you want to define a function that is called before the main function, you can add a phrase "__attribute__ (constructor)" After the declaration of the function, as follows:
int before () __attribute__ ((constructor));
If you want to define a function that is called after the main function, you can add a phrase "__attribute__ (destructor)" After the declaration of the function, as follows:
int after () __attribute__ ((destructor));
As you can see, it should be similar to the constructs and destructors in C + +.

Some of the details:
Write test code to test the program, found a few:
1, before before main call, before the call, the individual global variables have completed initialization. In other words, these functions are called before the main function after the global variable is initialized. This is very important, otherwise it may cause a lot of problems.
2, after the main after the call, but a little more special, must be in main return words before the execution, otherwise, you need to execute a function through atexit. This feature is not of much use to me at present.
3. A function called before the main function can be declared static.
4. A function called before the main function can invoke more than one. There is a problem here, the order in which these functions are called. The problem is, first of all, a design problem, that is, we should design functions that are not order-independent. In addition, the call order is related to the order of compilation, and I compile with make on Linux and find that the function in the last compiled source file will be invoked first.
5, you can define such a function in the library (dynamic library and Static library).

use of the role of the design:
1. Can optimize the single piece mode in C + +. Reference to the design pattern
One of the biggest features of a single piece mode is that it can be connected to a single piece during operation. If you use a conditional statement to decide which single piece to use, you can rigidly qualify a single set of possible pieces. Therefore, the book introduces the concept of a single piece of the Registry, the book on the initialization of a single registry using the following approach:
First, define a single piece class, in the constructor of a single piece of class, call the registration function of a single piece to register itself:
Copy Code code as follows:

Mysingleton::mysingleton ()
{
...
Singleton::register ("Mysingleton", this);
}

How is this function called? You can define a static instance:
static Mysingleton Thesingleton;
This will then invoke the Mysingleton constructor before the main function to construct this static instance to achieve the purpose of registry registration.
One drawback of this scheme is that it succeeds in the premise that a single registry list must exist before Thesingleton is instantiated, or it will fail. is actually just a possible point of failure, and if Mysingleton also applies other global variables, then these global variables may not be initialized at this time.
One solution to this problem is to move the time of a single piece registration from the constructor to the function called before the main function.
To define a function:
Copy Code code as follows:

static void Before_main ()
{
Singleton::register ("Mysingleton", &thesingleton);
}
Statement:
STAITC void Before_main () __attribute__ ((constructor));

Before_main is called before the main function, and the global variable is initialized all at the time of the call, which avoids the problem above.
In fact, a single piece can be used not only in C + + (object-oriented), but also in C. And with this feature of C, it's better to use a single piece.
2. Construct the plug-in development framework without making changes to the framework.
One problem with constructing a plug-in development framework is how to add a plug-in without modifying the main frame code to invoke the plug-in code. The plug-in registration mechanism is generally used. That is, the framework provides an external registration interface that plug-ins use to register. C to implement this function, a feasible solution is to define the function that executes before main in the plug-in, in which the plug-in registration interface is called to complete registration. (Note: The static loading of Plug-ins is discussed here.)
3, a module has some initialization work to do, use this mechanism can not change main or function.
With this feature, you can optimize the modularity of C by throwing away the plugin framework. For example, the initialization of each module can be placed before main to prevent frequent changes to main.
Note: The environment described in this article is Linux c,c++.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.