C/C ++ inline functions and extern "C"

Source: Internet
Author: User

Today we will discuss several questions about C/C ++.

Inline functions

When it comes to inline functions, let's take a look at the following twoProgramThe program execution enchantment is to get the sum of the two values:

 //  Procedure 1  
Int Add ( Int X, Int Y)
{
Return X + Y;
}

Int Main (Int Argc, Char * Argv [])
{
Int A = 1 , B = 2 , C;
C = add (A, B ); // Obtain the addition result by calling the function.
Return 0 ;
}

// Procedure 2
Int Main ( Int Argc, Int Argv [])
{
Int A = 1 , B = 2 , C;
C = A + B; // Directly calculate and obtain the sum result.
Return 0 ;
}

Which of the above programs has higher execution efficiency?

The answer is that "Program 2" is c = a + B and the result of adding is directly calculated, while "program 1" is the result of adding by calling the function c = add (A, B. The Direct Calculation of "Program 2" saves the overhead of function calling, parameter passing, control transfer, and so on, and the execution efficiency is much faster than that of function calling of "program 1.

However, in actual work, the function call method of "program 1" may be adopted more, because in terms of function encapsulation and reuse, A common program segment is usually encapsulated to be called in other places where the same function is required. This creates a conflict.CodeEncapsulating a function greatly reduces program efficiency.

To solve this problem, C ++ introduces the concept of inline functions. Inline functions provide a solution: If a function is specified as an inline function, it is expanded "inline" on the call point, the inline function does not control the transfer when it is called, but embeds the function body in the call place during compilation.

This means that when writing code, you can use the function call form of "program 1". When compiling, the compiler will compile "program 1" into "Program 2, this is the advantage of inline functions. The only thing to do is to add the keyword "inline" in "program 1.

 

  //   Program 3   
inline int Add ( int X, int Y) /// declared as an inline function
{< br> return X + Y;
}< br> int main ( int argc, char * argv [])
{< br> int A = 1 , B = 2 , C;
C = add (, b);
return 0 ;< BR >}

Note: the so-called inline function is just a suggestion for the c ++ compiler. The C ++ compiler can choose to ignore this suggestion. For example, for a function with more than 1200 rows, a function called recursively, or a function called recursively, generally, C ++ compilers do not compile them into inline functions.

Extern "C"

This code is often seen in the C ++ code:

 

 
# Ifdef _ cplusplus
Extern "C"{
# Endif

//Code segment

# Ifdef _ cplusplus
}
# Endif

What does this code mean? First, _ cplusplus is a custom macro in C ++. If this macro is defined, it indicates that this is a piece of C ++ code. That is to say, the meaning of the above Code is: if this is a piece of C ++ code, add extern "C" {And} to process the code.

To understand why extern "C" is used, you must start with the overload function in C ++. In C ++, in order to support the overload mechanism, some processing should be performed on the function name in the compilation code, such as the return type of the function. In C, it is just a simple function name and no other information is added. That is to say, C ++ and C process the name of the generated function differently. Therefore, in one sentence, adding extern "C" is to prevent name adaptation.

C and C ++ process functions differently. Extern "C" is a means to enable C ++ to call library files written by C. If you want to prompt the compiler to use C to process functions, use extern "c" to describe.

 

 


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.