Side Effects of macros

Source: Internet
Author: User

Question 3: Write a "standard" macro min, this macro enters two parameters and returns the smaller one. Also, what happens when you write the following code.

least = MIN (*p++, b);

Answer:

#define MIN (A,b) ((A) <= (B)? (A): (B))

MIN (*p++, b) can create side effects of macros

Analysis:

This question mainly examines the interviewer's use of the macro definition, a macro definition can implement functions similar to a function, but it is not a function at all, and the "arguments" in parentheses in a macro definition are not real arguments, and a one-to-one substitution is made for "parameters" when the macro is expanded.

Programmers should be very careful about using macro definitions, with particular attention to two issues:

(1) Carefully enclose the "parameters" and the entire macro in the macro definition in parentheses. So, strictly speaking, the following answers:

#define MIN (A,b) (A) <= (B)? (A): (B)

#define MIN (a,b) (A <= B?) A:B)

Should be sentenced to 0 points;

(2) To prevent the macro side effects.

Macro definition #define MIN (a,b) ((A) <= (B)? (A): (b) The result of the effect on min (*p++, b) is:

((*p++) <= (b)? (*p++): (*p++))

This expression will have side effects, and the pointer P will do three + + self-increment operations.

In addition, another answer that should be 0 points is:

#define MIN (A,b) ((A) <= (B)? (A): (B));

This solution adds ";" to the macro definition, showing that the creator's concept of the macro is ambiguous and can only be ruthlessly sentenced to 0 points and eliminated by the interviewer.

Eliminate side effects of macros

#include <stdio.h>
#define Min_i (x, y) ((x) < (y)? (x): (y))//(1)
#define MIN_T (type, x, y) ({type __x = x; \//(2)
Type __y = y; \
__x < __y? __x: __y; \
})
#define MIN (x, y) ({const typeof (x) _x = (x); \//(3)
Const typeof (Y) _y = (y); \
(void) (&_x = = &_y); \//(4)
_x < _y? _x: _y; \
})

int main ()
{
int a = 10;
int B = 20;
printf ("Min_i (a++, b++) =%d\n", Min_i (a++, b++)); 11
printf ("A =%d\n", a); 12
printf ("B =%d\n", b); 21st

A = 10;
b = 20;
printf ("min_t (int, a++, b++) =%d\n", min_t (int, a++, b++)); 10
printf ("A =%d\n", a); 11
printf ("B =%d\n", b); 21st

A = 10;
b = 20;
printf ("min (a++, b++) =%d\n", min (a++, b++)); 10
printf ("A =%d\n", a); 11
printf ("B =%d\n", b); 21st

}


(1). This definition calculates x and y respectively two times (the small in x and Y are counted two times), and when the parameter has side effects, it produces an incorrect result
(2). Use statement expressions to calculate only one argument at a time, avoiding possible errors; Statement expressions are typically used for macro definitions
(3). typeof (X) represents the value type of x
(4). Check that the types of parameters x and Y are the same (if the types of x and Y are different compilers will emit warning and do not affect the execution of subsequent statements)

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.