inline functions (inline) in C language and macro definitions (#define) detailed parsing of _c languages

Source: Internet
Author: User
Tags function definition
First, the key:
1,
Inline functions are the same in readability as functions, while at compile time the functions are embedded directly into the body of the caller, eliminating the call/return instruction, which is faster at run time.

2,Inline functions can be debugged, and macro definitions cannot be debugged.
Inline functions and macros are essentially two different concepts if a program writer has a requirement for both fast and readable conditions, the function should be crowned inline. The following is a detailed discussion of inline functions and macro definitions.

What is an inline function?
An inline function is a function that the code is inserted at the caller's code. As with #define macros (but not the same as the reason below), inline functions improve execution efficiency by avoiding the overhead of being invoked, especially if it can be optimized by the compiler by invoking ("Process integration").

Second, how does inline function achieve compromise on security and speed?
In C, you can get "encapsulated structure" by setting a void* in the structure, in which case the void* pointer to the actual data is unknown to the user of the structure. Therefore, the user of the struct does not know how to interpret the contents of the void* pointer, but the Access function can convert the void* to the appropriate implied type. This gives a form of encapsulation.

Unfortunately, this has lost type safety and also imposes tedious access to function calls on every domain in the structure. (If you allow direct access to the domain of the struct, it is necessary for anyone with direct access to understand how to interpret the contents of the void* pointer; This will make it difficult to change the underlying data structure).

Although the function call overhead is very small, it accumulates. C + + classes allow function calls to expand inline. This gives you the speed of direct access while getting the security of the package. In addition, the parameter types of inline functions are checked by the compiler, which is an improvement to the #define macros of C.


Three, why should I use inline function? Rather than the original clear #define macros?
Because #define macro-definition functions are harmful everywhere:
Unlike #define macros, inline functions always evaluate the parameters exactly once, thereby avoiding the infamous macro error. In other words, calling an inline function is equivalent to calling a normal function, and the difference is only faster:
Copy Code code as follows:

A macro that returns the absolute value of I
#define UNSAFE (i) \
((i) >= 0? (i):-(i))

Returns the inline function of the absolute value of I
Inline
int safe (int i)
{
Return i >= 0? I:-i;
}

int f ();

void Usercode (int x)
{
int ans;

Ans = unsafe (x + +); Error! X was added two times
Ans = unsafe (f ()); Dangerous! F () is called two times

Ans = Safe (x + +); That's right! X is added one time
Ans = Safe (f ()); That's right! F () is called once
}

Unlike macros, the parameter types of inline functions are checked, and the necessary transformations are made correctly. Macros define complex functions as harmful;

How do I tell the compiler to make a non-member function an inline function?
Declaring inline functions looks very similar to normal functions:
void f (int i, char c);
When you define an inline function, add the inline keyword before the function definition, and put the definition in the header file: inlinevoid f (int i, char c) {//...}
Note:The definition of the function ({...} In the header file is mandatory unless the function is only used by a single. cpp file. In particular, if you place the definition of an inline function in a. cpp file and call it in a different. cpp file, the connector will give a "unresolved external" error.

How do I tell the compiler to make a member function an inline function?
Declaring an inline member function looks very similar to a normal function:
Class Fred {public:
void f (int i, char c);
But when you define inline member functions, add the inline keyword before the member function definition and put the definition in the header file: inlinevoid fred::f (int i, char c) {//...} Typically, the definition of a function ({...} In the header file is mandatory. If you place the definition of an inline function in a. cpp file and call it in a different. cpp file, the connector will give a "unresolved external" error.

is there another way to tell the compiler to make a member function inline?
There are: Define member functions in the class body: class Fred {public:   void f (int i, char c)      {    & nbsp; //...    }}; Although this is easy for people who write classes, it mixes the class with "what" (what) and class "how". Summary in summary, in embedded C (or C + + Programming, it's good for us to know how to use inline functions (inline) and macro definitions (#define) and use them well. (Note: This part of the content from the network collation, the above discussion belongs to personal opinion, for reference only.) The wrong place is inevitable! )
Related Article

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.