In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what is a macro definition function, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
During preprocessing, all "macro names" that appear in the program are replaced with strings in the macro definition, which is called "macro substitution" or "macro expansion". The macro definition is done by the macro definition command in the source program. Macro substitution is done automatically by the preprocessor. If the string is an expression, we call it a functional macro definition.
Let's take the following two lines of code as an example to expand the description:
Functional macro definition: # define MAX (aMagneb) ((a) > (b)? (a): (b))
Common function: MAX (a _ r _ b) {return a > bounded _ b;}
(1) the parameters defined by functional macros have no type, and the preprocessor is only responsible for formal replacement, but not parameter type checking, so you should be extra careful when passing parameters.
(2) the definition of functional macros should pay attention to the format, especially parentheses.
If the above macro definition function is written as # define MAX (a b?a:b b) (a > parenthesis), the error occurs after the macro expansion due to operator priority; if the above macro definition function omits the outer parentheses, and the macro definition is + + MAX (a), then the macro expansion becomes + + (a) > (b): (b), the operation priority is also wrong.
(3) if the function parameter is an expression, the call of the ordinary function is different from the replacement process defined by the functional macro.
When an ordinary function is called, the value of the argument expression is first obtained and then passed to the parameter. If the argument expression has SideEffect, then the SideEffect occurs only once. For example, MAX (+ + a, + + b), if MAX is an ordinary function, an and b are incremented only once. But if the MAX functional macro definition is expanded to k = ((+ a) > (+ + b)? (+ + a): (+ + b)), an and b are not necessarily added once or twice. So if the parameter is an expression, be sure to watch carefully when replacing the functional macro definition.
(4) the code that calls the real function is different from the code defined by the functional macro.
If MAX is an ordinary function, then its function body return a > b? A: B; to compile the build instruction, each call that appears in the code also compiles the generate parameter instruction and the call instruction. If MAX is a functional macro definition, the macro definition itself does not have to compile the generated instruction, but each call to compile the generated instruction in the code is equivalent to a function body, rather than a few simple parameter instructions and call instructions. Therefore, the target file generated by compilation using functional macro definitions will be larger.
Advantages:
First of all, a function call brings extra overhead, which needs to open up a stack space, record the return address, press the stack, and release the stack from the function return, which reduces code efficiency. Using macro definitions is better than functions in terms of code size and speed.
Second, the argument of a function must be declared as a specific type, so it can only be used on expressions of the appropriate type. If we want to compare the sizes of two floating-point types, we have to write a comparison function specifically for the size of the floating-point type. On the contrary, the above macro definition can be used for integers, long integers, single-floating-point, double-floating-point, and others. "
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.