C language inline function of of inline and macro definition of of hashdefine detailed resolution

  • 2020-04-02 01:41:08
  • OfStack

Let's get to the point:
1.
Inline functions are the same as functions in terms of readability, while at compile time they embed the function directly into the body of the calling program, eliminating the call/return instruction, which makes them faster at run time.

2, Inline functions can be debugged, but macro definitions cannot.
Inline functions and macros are essentially two different concepts. If a program writer wants it to be both fast and readable, he should use inline. The following sections explore inline functions and macro definitions in more detail.

What is an inline function?
An inline function is a function where the code is inserted into the caller's code. Like the #define macro (but not the same, for reasons described below), inline functions improve execution efficiency by avoiding the overhead of being called, especially since they can be optimized by the compiler through invocation (" procedural integration ").

2. How do inlined functions compromise safety and speed?
In C, you can get the "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. So the user of the structure does not know how to interpret the void* pointer to the content, but the accessor function can convert the void* to the appropriate implicit type. This gives a form of encapsulation.

Unfortunately this loses type safety and imposes tedious access to each domain in the structure on function calls. If you allow direct access to the fields of a structure, it is necessary for anyone with direct access to know how to interpret the void* pointer. This will make it difficult to change the underlying data structure.

Although the function call overhead is small, it accumulates. C++ classes allow function calls to be expanded inline. This gives you the speed of direct access while still having encapsulated security. In addition, the argument types of inline functions are checked by the compiler, which is an improvement on the C #define macro.

 
Why should I use inline functions? Instead of the clear #define macro, right?  
Because the #define macro definition function is harmful everywhere:
Unlike the #define macro, inline functions always evaluate arguments exactly once, avoiding the notorious macro error. In other words, calling inline functions is equivalent to calling normal functions, the difference is simply faster:

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

//An inline function that returns 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 is increased twice
   ans = unsafe(f());   //Danger! F () is called twice

   ans = safe(x++);     //Right!!! X is increased once
   ans = safe(f());     //Right!!! F of alpha is called once
}

Unlike macros, the argument types of inline functions are checked and the necessary conversions are performed correctly. Macros are harmful for defining complex functions; Don't use it unless you have to.

How to tell the compiler to make non-member functions inline?
Declaring inline functions looks very similar to normal functions:
Void f(int I, char c);
When you define an inline function, insert the inline keyword before the function definition and put the definition in the header file: inlinevoid f(int I, char c){     / /... }
Note: Define the function ({... It is mandatory that the function be placed in the header file, unless the function is used only by a single.cpp file. In particular, if you put the definition of an inline function in a.cpp file and call it in another.cpp file, the connector gives the "unresolved external" error.

How to tell the compiler to make a member function inline?
Declared inline member functions look very similar to normal functions:
The class Fred {public:    
Void f(int I, char c); };
But when you define an inline member function, insert the inline keyword before the member function definition and put the definition in the header file: inlinevoid Fred: : f (int I, char c) {     / /... } usually defines a function ({... It is mandatory for the section between}) to be placed in the header file. If you put the definition of the inline function in the.cpp file and call it in another.cpp file, the connector will give the "unresolved external" error.

Is there another way to tell the compiler to inline member functions?
Class Fred {public:     Void f(int I, char c)         {             / /...         }}; Although this is easy for people who write classes, it is useful to know how to use inline and macro definitions in embedded C (or C++) programming. (note: part of this paper is from the network, the above discussion is personal opinion, for reference only. Mistakes are inevitable!)

Related articles: