[C + +] defines a function with a macro

Source: Internet
Author: User

Important: The variables are enclosed in parentheses to prevent errors and end up not required; In practical programming, it is not recommended to use macros for complex functions, which is not easy to debug. Multi-line use \

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".

Property

#define宏

Function

Code length

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

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

Execution speed

Faster

There is a function call, the extra overhead of returning

Operator Precedence

The evaluation of macro parameters is in the context of all surrounding expressions, unless they are enclosed in parentheses, the precedence of the neighboring operator may produce unpredictable results.

The function parameter is evaluated only once at the time of the function call, and its resulting value is passed to the function. The evaluation result of an expression is more predictable.

Parameter evaluation

When parameters are used for macro definitions, they are re-evaluated each time, and due to multiple evaluation, parameters with side effects can produce unpredictable results.

Arguments are used only once before a function call, and multiple use of parameters in a function does not result in multiple evaluation procedures, and the side effects of the parameters do not cause any special problems.

Parameter type

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

The parameters of the function are related to the type, and if the arguments are of different types, different functions need to be used, even if the tasks they perform are the same.

[C + +] defines a function with a macro

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.