Article From:

When you see the comparison between inline functions and macrodefinition functions, you can’t help thinking about what macros define functions. “”

Original address:

————————Text segmentation line – — – — – —-

Main points:Variables are enclosed in parentheses to prevent errors and end.。In practical programming, it is not recommended to use macros for complex functions, and it is not easy to debug.Multiline use

To write C language, beautiful macro definition is very important. Macro definition can help us prevent mistakes and improve portability and readability of code.

In the process of software development,Often there are commonly used or universal functions or code segments, which can be written as functions or encapsulated as macros.。So is it a good function or a macro definition? This requires us to make a reasonable choice between the two party.

Let’s take an example to compare the two numbers or the size of the expression. First we write it as a macro definition:

#define MAX(a, b) ((a) > (b) ? (a):(b))

Secondly, it is realized by function:

int max(int a, int b)
  return (a > b ? a : b);

Obviously, we will not choose to use functions to accomplish this task, for two reasons: first,The function call will bring additional cost. It needs to open up a stack space, record the return address, stack the parameter, return from the function and release the stack.。This overhead will not only reduce the efficiency of the code, but also greatly increase the amount of code.Using macrodefinition is superior to function in terms of code size and speed.;Secondly,The parameter of a function must be declared to be a specific type, so it can only be used on a suitable expression.,If we want to compare the size of two floating points, we have to write a comparison function specifically for floating point type. On the other hand, the above macro definition can be used for shaping, long plastic, single floating-point, double floating-point, and any other type that can be used to compare the value of the “>” operator, that is,Macros are type independent.

Compared with the use of functions,The disadvantage of using macros is that a macro definition code copy is inserted into the program every time you use macros.。Unless macros are very short, using macros greatly increases the length of programs.

There are other tasks that can’t be implemented by functions at all, but with macro definition, they can be implemented very well. For example, the parameter type can not be passed to the function as a parameter, but the parameter type can be passed to the macros with the reference.

Look at the following examples:

#define MALLOC(n, type) \
((type *) malloc((n)* sizeof(type))

With this macro, we can allocate a specified size of space for any type and return a pointer to this space. We can observe the exact working process of this macro:

int *ptr;
ptr = MALLOC( 5, int );

After this macro is expanded, the result is:

ptr = (int *) malloc ( (5) * sizeof(int) );

This example is one of the classic applications of macro definition, which completes the function that the function can not be completed, but the macro definition can not be abused.If the same code needs to appear in several parts of the program, the better way is to implement it as a function.

The difference between macro and function is summarized below for use when you write code. This summary is from C and pointer.




Code length

The macro code is inserted into the program every time it is used. In addition to very small macros, the length of the program will increase significantly.

Function code only appears in one place: every time you use this function, you call the same code in that place.

Execution speed


The additional overhead of the existence of function calls and returns

Operator precedence

The evaluation of macro parameters is in the context of all the surrounding expressions.Unless they are brackets, the priority of adjacent operators may produce unpredictable results.

The function parameter is evaluated only once in function call, and its result value is passed to function. The result of the evaluation of the expression is easier to predict.

Parameter evaluation

When a parameter is used for a macro definition,Each time will be reevaluated,Because of repeated evaluation, parameters with side effects may produce unpredictable results.

The parameter is only once before the function call, and the multiple use of parameters in the function does not lead to the multiple evaluation process. The side effects of the parameters do not cause any special problems.

Parameter type

Macros are independent of type. As long as the operation of parameters is legal, it can be used for any parameter type.

The parameters of a function are related to the type. If the type of the parameter is different, you need to use different functions, even though the tasks they perform are the same.

Leave a Reply

Your email address will not be published. Required fields are marked *