This is the 16th day of my participation in the August More Text Challenge
What is a macro definition?
Macro definition is one of the three preprocessing functions provided by C, including macro definition, file inclusion, and conditional compilation. We often see a lot of macro definition code in IOS, even in apple’s official documentation macro definitions are a big part of the code.
2. Use of macro definitions
Macro definition is also called macro substitution, macro substitution, or “macro” for short.
Format:
#defineIdentifier string
Copy the code
The identifiers are called symbolic constants, also known as macro names.
The preprocessing (precompilation) work is also called macro expansion: replacing the macro name with a string.
The key to mastering the concept of macros is change. Everything on the premise of change, before doing anything to change, before understanding accurately “change”. That is, before a specific analysis of the meaning and function of the relevant command or statement:
Ex. :
#define3.1415926 Pi?
Copy the code
Change all Pi in the program to 3.1415926
Description:
- (1) Macro names are generally used
A capital
- (2) the use of macros can improve the program
generality
andlegibility
, reduce inconsistencies, reduce input errors and facilitate modification. For example, array sizes are commonly defined by macros - (3)
pretreatment
Is in thecompile
And one of the tasks of compiling isSyntax checking
, preprocessing does not do syntax checking. - (4) No semicolon at the end of macro definition;
- (5) The macro definition is written outside the curly braces of the function and scoped to the following program, usually at the beginning of the file.
- (6) Yes
#undef
The command terminates the scope of the macro definition - (7) Macro definitions allow nesting
- (8) String
""
Macros are never included in the - (9) Macro definitions do not allocate memory, variable definitions allocate memory.
- (10) The macro definition has no type problem and its parameters are untyped.
3. Strengths and weaknesses of macro definitions
Advantages:
- (1) Improved procedures
readability
And it is also easy to modify - (2) Improve the running efficiency of the program, using macro definition with parameters can complete the function of function call, and can avoid the function stack and stack operation, reduce system overhead, improve the running efficiency
- (3) Macros are made by
The preprocessor
String manipulation can do a lot of things that the compiler can’t do, for example# # link
Disadvantages:
- (1) Because it is directly embedded, the code may be relatively more
- (2) too many nested definitions may affect the program
readability
And it’s easy to make mistakes - (3) with parameters
The macro
Since it is a direct substitution, it does not check whether the parameter is valid and existsSafe hidden trouble
Supplement:
Precompiled statements are simply value substitutions without a type detection mechanism, so precompiled statements do not enjoy the benefits of C++ ‘s rigorous type checking and can be a potential source of errors.
Finally, a sentence from “C Trap and Defects” is quoted to summarize it:
Macros are not functions. Macros are not statements. Macros are not type definitions
4. Constant macros define the replacement scheme const
Const: Generally, strings are defined as const. (Apple recommends using const for constant strings.)
5, the difference between macros and const
- (1) Compilation time is different. Macros are precompiled and const is compile-time
- (2) Macros can define code, const can’t. Multiple macros take longer to compile, which affects development efficiency and slows debugging. Const will compile only once, shortening compilation time.
- (3) Macros don’t check for errors, const does
6. Introduction of inline functions
Inline functions from the source layer, function structure, and in the compiled, but does not have function properties, inline function is not in the call control transfer, but at compile time to embed the function body into each call, at compile time, similar to the macro substitution, using the function body to replace the call to place the function name, the code using the inline keyword, Member functions declared in a class are automatically converted to inline functions.
- Member functions in a class are inlined by default. (Member functions are required to have no loops and recursion. When recursive loops are present, the compiler treats them as normal functions by default.)
- Inline functions are not allowed to have loops or recursive statements
- The definition of an inline function must appear before the first call to the inline function
Such as:
#pragmaMark determines if the device is an iPhone X
static inline BOOL iPhoneX() {
BOOL iPhoneXSeries = NO;
if (UIDevice.currentDevice.userInterfaceIdiom ! =UIUserInterfaceIdiomPhone) {
return iPhoneXSeries;
}
if (@available(iOS 11.0, *)) {
UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
if (window.safeAreaInsets.bottom > 0.0) {
iPhoneXSeries = YES; }}return iPhoneXSeries;
}
Copy the code
7. Advantages and disadvantages of inline functions
Advantages:
- Parameter type detection, more secure
- Inline functions are expanded while the program is running and pass arguments
- The inline keyword is simply a definition for the compiler, which treats the function as a normal function if it does not meet the criteria for inline functions locally
Disadvantages:
- Because inline functions are expanded at the call point, they make the code longer and take up more memory
8. The difference between inline functions and macros
- Inline functions are debuggable at run time, while macro definitions are not
- The compiler does security checks or automatic type conversions on inline function parameter types, whereas macro definitions do not
- Inline functions have access to class member variables, whereas macro definitions do not
In the future, if we define a constant string, we can use const. We can define code using macros or inline functions.