Comparison and Analysis of inline functions and common functions:

Source: Internet
Author: User

Author: closeall
Source: http://blog.csdn.net/closeall2008
Disclaimer: This article may be reproduced or reproduced without the consent of the author. However, please keep the author, source, and declaration of any reference to this article. Thank you.

We know that C ++ has expanded the C language through inline functions, so as to change the running efficiency of the program to some extent.

. However, the difference between common functions and inline functions lies not in the way they are written, but in the understanding of them by the C ++ compiler,

Description. To have a deep understanding of inline functions, we must go deep into the functions.

We know that after the code is compiled by the compiler, the program statements we write are translated into machine commands that can be recognized by machines. However

To generate executable programs. When the program runs, the system transfers the machine commands to the memory, so each command has

Address. The machine executes these machine commands in the logical order of your program, but not every command runs. Some commands

Will be skipped, for example, some commands in the IF statement. Therefore, the execution of commands is not sequential, and there are some jumps. Soupe

How does a common function run? The following is an analysis: when the program runs the command to call a function

The address is recorded, and then the address of the function is passed (any instruction will have a memory address, and of course the function is no exception)

It will jump to the instruction place when the function itself is defined, copy the actual parameter passed to the function, and pass it to the function's form parameter into the stack, and then

Execute the Internal commands of the function. After the Internal commands of the function are executed, return to the original

To continue executing the following command. This is because operations such as recording the address and redirection exist during the execution of common function calls.

There are some additional overhead. Inline functions use another mechanism, which is not used for calling common functions. When the command is executed

When an inline function is used, it does not record the address and jump operations. Instead, it is equivalent to copying the Internal commands of the inline function in the called

Place, and the execution of the sequence goes down. In this way, the inline function does not have a jump, so the execution speed is a little faster than the call speed of common functions.

But it will occupy more memory. The more times the inline function is called, the larger the memory will be occupied, because every

Where an inline function is called, it is equivalent to a copy of the inline function. If 10 inline functions have to be called

There are 10 inline function copies in the order, and 100 are called and 100 are copied. Therefore, inline is usually used for small functions.

Function. Another point is that if the function itself is executed for a long time and the time saved by using the inline function is

In this case, do not use inline functions. After all, the function is too large to occupy a lot of memory space. Also

Some of the content is relatively easy to ignore. The following are some points for me: first, the compiler will not put every inline letter you define

The number is regarded as an inline function during compilation. This depends on the compiler. during compilation, the compiler will check your settings.

Whether the inline function meets the requirements, or if there are other reasons, the compiler depends on the situation. Second,

Inline functions do not allow recursion, and these 1.1 must be remembered. These two points are not mentioned in many books.

Note.

So where is the inline function defined? Defining inline functions is typically defined in header files. And,

Inline functions do not need to be declared. They define inline functions directly in the header file, which is equivalent to declarations.

And definition at the same time, we know that this is allowed, because it is in the header file, but we do not need to set in the header file

Any other function except the inline function.

I don't want to say much about how to define inline functions. Just add inline before the function, because the specific definition

It is easy to do. It will be explained in any basic book. In fact, for inline functions

I will not talk about some of his content here.

 

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.