Inline functions and macros

Source: Internet
Author: User

1.Inline functions and macros:

Inline extension is used to eliminate the time overhead for function calls. It is usually used for frequently executed functions. A function with a small memory space is very beneficial. So what is the relationship between inline functions and Macros ??

In fact, the functions of inline functions are similar to those of preprocessing macros. We often define some macros, such as # define table_comp (x)> 0? (X): 0) This defines a macro. Let's see why macro is used? Because function calls mustProgramThe execution sequence is transferred to an address stored in the memory of the function. After the program content of the function is executed, it is returned to the place before the function is executed. This transfer operation requires that the on-site address should be stored and remembered before the transfer is executed. After the transfer, the on-site address should be restored and the operation should be continued based on the original storage address. Therefore, function calling requires a certain amount of time and space overhead, which affects its efficiency. Macros are only used in preprocessing.CodeExpansion does not require additional space and time overhead. Therefore, it is more efficient to call a macro than to call a function. These benefits are the same as those of inline functions!

However, there are many unsatisfactory aspects of Macro.

1. Macros cannot access private members of objects.

2. The definition of macros is prone to ambiguity.

For example: # define table_multi (x) (x * X)

We use a number to call it, table_multi (10). It seems that there is no error. The result returns 100, which is correct, but if we use table_multi (10 + 10) the expected result is 400, while the macro call result is (10 + 10*10 + 10) and the result is 120. This is obviously not the expected result. To avoid these errors, add brackets to macro parameters.

# Define table_multi (x) * (x) ensures that no error occurs. However, even if this definition is used, this macro may still fail, for example, if table_multi (A ++) is used to call it, they want to get the result (a + 1) * (a + 1). What is the actual result? Let's take a look at the macro expansion result (A ++) * (a ++). If the value of A is 4, the result is 4*4 = 16, A = 6. The expected result is 5*5 = 25, which causes another problem.

We can see that macro has some unavoidable problems. How can we solve them? This requires the introduction of restrained functions. We can use inline functions to replace macro definitions. In fact, we can replace the pre-processing macro With inline functions. The difference between an inline function and a macro is that a macro is replaced by a Preprocessor, while an inline function is implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues.

 

2.Use of inline functions:

We can use inline to define inline functions. However, any function defined in the description section of the class will be automatically considered as an inline function. Next we will introduce the usage of inline functions. Inline functions are valid only when they are declared together with the function body. An inline tablefunction (int I) statement like this is ineffective. The Compiler just uses the function as a general statement of the number of functions. We must define the function body.

Inline tablefunction (int I) {return I * I };

This defines an inline function. We can call it as a common function. However, the execution speed is indeed faster than that of common functions. We can also define external functions defined in the class as inline functions, such:

Class tableclass {

PRIVATE:

Int I, J;

Public:

Int add () {return I + J ;};

Inline int Dec () {return I-j ;}

Int getnum ();

}

Inline int tableclass: getnum (){

Return I;

}

All the three functions stated above are inline functions. In C ++, the function of the function body is defined inside the class and is considered as an inline function by default. Whether or not you have the inline keyword. Inline functions are the most widely used in C ++ classes and should be used to define access functions. Generally, data members are defined as private or protected in the classes we define. In this way, the outside world cannot directly read and write the data of our class members. To read and write private or protected members, you must use the member interface function. If we define these read/write member functions as inline functions, the efficiency will be better.

Class sample {

PRIVATE:

Int ntest;

Public:

Int readtest () {return ntest ;}

Void settest (int I) {ntest = I ;}

}

3.Limitations of inline functions:

Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions. When using inline functions, pay attention to the following points:

1. Loop statements and switch statements are not allowed in inline functions. If an inline function has these statements, compile the function as a common function to generate the function call code.

2 recursive functions (self-called functions) cannot be used for inline functions.

3. the inline function is only applicable to 1 ~ Five rows of small functions. For a large function that contains many statements, the overhead of function calling and return is relatively insignificant, so it is not necessary to use inline functions.

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.