In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of macros and inline functions in the programming language, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.
Part one: macros
Why use macros?
Because the call of the function must transfer the order of the program execution to an address where the function is stored in memory, after the execution of the program content of the function, and then return to the place before the transfer to execute the function. This kind of transfer operation requires that the site should be saved and the execution address should be remembered before the transfer is carried out, and the site should be restored after the transfer back, and the execution should continue according to the original saved address. Therefore, the function call must have a certain amount of time and space overhead, which will affect its efficiency.
While macros only expand the code in the place of preprocessing, there is no additional space and time overhead, so calling a macro is more efficient than calling a function.
But Acer also has a lot of unsatisfactory places.
1. Macros cannot access private members of the object.
2. The definition of macros can easily lead to ambiguity.
Let's give an example:
# define square (x) (xxxx)
We call it with a number, square (5), so there seems to be no error, and the result returns 25, which is correct, but if we call it with squre (5: 5), we expect the result to be 100, while the macro call is (5: 5, 5: 5), and the result is 35, which is obviously not the result we want to get. The first way to avoid these errors is to add parentheses to the parameters of the macro.
# define square (x) ((x) * (x))
Part two: inline function
From the above, we can see that macros have some unavoidable problems, how to solve them?
An inline function is a function where the code is inserted into the caller code. Like the # define macro, inline functions improve execution efficiency by avoiding the overhead of being called, especially when they can be optimized by the compiler through calls ("procedural integration").
Inline functions are very similar to macros, except that macros are replaced by preprocessors, while inline functions are controlled by the compiler. And the inline function is a real function, but when needed, the inline function expands like a macro, so it cancels the parameter stack of the function and reduces the overhead of the call. You can call inline functions like calling functions without having to worry about some of the problems that arise from handling macros.
Declaring inline functions looks very similar to ordinary 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:
Inline void f (int I, char c)
{
/ /...
}
An inline function must be declared with the function body to be valid.
Declaring inline function (int I) like this is useless, the compiler just declares the function as a normal function, and we have to define the function body.
Inline int function (int I) {return itemi;}
In this way, we define an inline function. We can call it as a normal function. However, the execution speed is faster than that of ordinary functions.
Of course, inline functions also have some limitations. That is, the execution code in the function can not be too much. If the function body of the inline function is too large, the general compiler will give up the inline method and call the function in the normal way. In this way, inline functions are as efficient as ordinary functions.
With the features of both above, we can completely replace preprocessing macros with inline functions.
Thank you for reading this article carefully. I hope the article "sample Analysis of Macros and inline functions in programming languages" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.