First, #define
1. Just as an identifier:
#define作为标识符时是没有参数的
2, as a macro:
the difference between #define as a macro and it as an identifier is that the macro has parameters and the identifier does not
For example: #define MAX (x, y) > (Y)? ( X):(Y))
The difference between a macro and a function
1, macro in use (compile phase) will be directly replaced (that is, simple code copy), and will not be logical detection
2, in the code debugging, can not enter the internal debugging, after the execution of the direct return to the resulting value. And the function differs from this
3. The macro does not take into account the type of the parameter when it is defined (but this is not safe)
4. Macros will produce macro parameters with side effects
Please analyze the following code:
#include <stdio.h> #include <stdlib.h> #define MAX (x, y) (() > (Y)? ( X):(Y)) int main () {int a = 10;int b = 20;int ret = max (a++,b++);//int ret = max (a++,b++) (a++) > (b++)? ( a++):(b++)) printf ("a=%d,b=%d,ret=%d\n", A,b,ret); System ("pause"); return 0;}
The resulting result is: a=11,b=22,ret=21
The reason for this result is that the macro was replaced with a side effect parameter, a was executed once, B was executed twice
5, the parameter macro runs faster than the function, because it is just a simple replacement, do not need to stack/stack operation parameters
6, macro in the definition of as far as possible to add parentheses
For example: In a macro definition #define DOUBLE (x, y) x*y, if the parameter given when using is
(5+5,5+5), the result of the operation will be error.
7. The macro can use the type as a parameter, but the function is not possible (about dynamic memory)
Use a function to dynamically open a space:
int main () {int *p= (int *) malloc (10*sizeof (int)); int i=0;for (; i<10; i++) {p[i]=i;} for (i=0; i<10; i++) {printf ("%d", P[i]);} Free (p); system ("pause"); return 0;}
Cons: There may be a failure to open up memory. Example: int *p= (int *) malloc (0xFFFFFFFF);
0xFFFFFFFF is a collection of all memory address encodings, that is, all memory, in which case the open memory fails, so you should also judge the pointer p after using the malloc () function.
int i=0;int *p= (int *) malloc (10*sizeof (int)), if (p = = NULL) {printf ("Put of memory!\n"); exit (1); End Program}
The use of macros to open up memory will eliminate these problems:
#define MALLOC (Count,type) (type*) alloc (count*sizeof (TYPE)) void *alloc (int sz) {void *p=malloc (SZ); if (p = = NULL) { printf ("Out of memory!\n");} return p;} int main () {int *p=malloc (10,int); int i=0;for (; i<10; i++) {p[i]=i;} for (i=0; i<10; i++) {printf ("%d", P[i]);} Free (p); Be sure to release these memory after dynamic opening, otherwise the memory leak system ("pause") will occur; return 0;}
When you open up memory, just pass in the type and it's OK.
8, macros can not be recursive, function can be
#define MAX (x, y) > (y)? ( X):(Y)) #define M 100int Main () {int ret = MAX (max (m,20), 200); This is nested use, not recursive return 0;}
9, function only in the target file exists in one place, compared to save space
10, when the macro definition, the name and parameters can not be separated by a space, but the use of a
About # define