To write a good C language, a beautiful macro definition is very important. Macro definitions can help us prevent errors, improve code portability and readability, and more.
In the software development process, there are often common or common functions or code snippets that can be written as functions or encapsulated as macro definitions. So is the function good or the macro definition good? This requires us to make a reasonable choice between the two.
Let's look at an example, compare two numbers or the size of an expression, and first we'll write it as a macro definition:
#define MAX (A, B) ((a) > (b) (a): (b))
Second, it is implemented using a function:
int max (int a, int b)
{
Return (a > B a:b)
}
Obviously, we don't choose to use functions to accomplish this task for two reasons: first, the function call brings extra overhead, it needs to open up a stack of space, record the return address, press the parameter stack, return from the function and release the stack. This overhead not only reduces the efficiency of the code, but also greatly increases the amount of code, while the use of macro definitions in terms of code size and speed is better than the function, and second, the function's parameters must be declared as a specific type, so it can only be used on the type of the appropriate expression, If we were to compare the size of two floating-point types, we would have to write a comparison function specifically for floating-point types. Conversely, the macro definition above can be used for shaping, long shaping, single float, double floating-point type, and any other type that can be used to compare value sizes with the ">" operator, that is, macros are type-independent.
The disadvantage of using a macro, compared to using a function, is that each time a macro is used, a copy of the macro definition code is inserted into the program. Unless the macro is very short, using a macro greatly increases the length of the program.
There are some tasks that cannot be implemented with functions at all, but are well-defined with macros. For example, parameter types cannot be passed as arguments to a function, but they can be passed to a macro with parameters.
Look at the following example:
#define MALLOC (n, type) \
((Type *) malloc ((n) * sizeof (type)))
With this macro, we can assign a space to any type we specify and return a pointer to that space. We can look at the exact working process of this macro:
int *ptr;
ptr = MALLOC (5, int);
Expand this macro to the following result:
PTR = (int *) malloc ((5) * sizeof (int));
This example is one of the classic applications of macro definition, the function can not be completed, but the macro definition can not be abused, usually, if the same code needs to appear in several parts of the program, a better way is to implement it as a function.
The following summarizes the differences between macros and functions for use when you write code, which is excerpted from the book "C and Pointers".
Example
Single-line definition of define
#define MAXI (A, B) (A>;B?A:B)
Multi-line definition of define
Define can replace multiple lines of code, such as the macro definition in MFC (very classic, although it makes people look disgusting)
#define
MACRO (arg1, arg2) do {\
\
STMT1; \
STMT2; \
\
} while (0)
The key is to add a "\" to each line break
Macro definition write out swap (x, y) swap function
#define SWAP (x, y) \
x = x + y;\
y = x-y;\
x = XY;
ZigBee multi-line define has the following example
#define Fillandsendtxoptions (Transseq, ADDR, ID, LEN, txo) {\
Afstatus_tstat; \
Zdp_txoptions = (txo); \
Stat = Fillandsend ((transseq), (ADDR), (ID), (LEN)); \
Zdp_txoptions =af_tx_options_none; \
Returnstat; \
}
C language macro definition and macro definition functions