The 46th lesson function and Macro analysis

Source: Internet
Author: User

1. functions and macros

(1) Macros are expanded by direct substitution of preprocessing , and the compiler does not know the existence of macros

(2) A function is an entity compiled directly by the compiler , and the invocation behavior is determined by the compiler

(3) Multiple use of macros will result in an increase in the volume of the final executable program

(4) The function is jump execution, in memory only one copy function body exists

(5) The efficiency of the macro is higher than the function , because it is directly expanded, no call overhead

(6) When a function call creates an activity record, it is less efficient than a macro

"Programming Experiment" functions and macros

#include <stdio.h>#defineRESET (P,len) while(Len >0)                  ((Char*) p) [--len] =0voidResetvoid* p,intLen) {     while(Len >0)      ((Char*) p) [--len] =0;}intMain () {intArray[] = {1,2,3,4,5}; intLen =sizeof(array); intI=0;  for(i=0; i<5; i++) {printf ("array[%d] =%d\n", I, array[i]);    } RESET (array, len); //Reset (array, len);     for(i=0; i<5; i++) {printf ("array[%d] =%d\n", I, array[i]); }    return 0;}

2. Limitations of macros

(1) The efficiency of the macro is slightly higher than the function, but its side effects are huge .

(2) macro is text substitution, parameter cannot be type checked

(3) Functions that can be done with functions, absolutely no macros

(4) A recursive definition cannot appear in the definition of a macro

Side effects of the "instance analysis" macro

#include <stdio.h>#define_add_ (A, b) A + b#define_mul_ (A, b) A * b#define_min_ (A, B) ((a) < (b)? (a): (b))intMain () {inti =1; intj =Ten; //The intention is to calculate 3 * 7printf"%d\n", _mul_ (_add_ (1,2), _add_ (3,4)));//1 + 2 * 3 + 4 ==>11//original request min (1,10)printf"%d\n", _min_ (i++, J));//(i++) < (j)? (i++):(B)//Output 2    return 0;}

3. The magical magic of the macro

(1) used to generate some code of the general nature

(2) encapsulation function , plus type information

The magical application of "example analysis" macro

#include <stdio.h>#include<malloc.h>#definemalloc (Type, x) (type*) malloc (sizeof (type) *x)#defineFree (p) (Free (p), p = NULL)//output Format: variable name = value of variable#defineLog_int (i) printf ("%s =%d\n", #i, i)#defineLog_char (c) printf ("%s =%c\n", #c, C)#defineLog_float (f) printf ("%s =%f\n", #f, F)#defineLog_pointer (p) printf ("%s =%p\n", #p, p)#defineLog_string (s) printf ("%s =%s\n", #s, s)//foreach function#defineFOREACH (i, N) while (1) {int i = 0, L = n;for (i=0;i<l;i++)#defineBEGIN {#defineEND}break;}intMain () {int* pi = MALLOC (int,5);//The return type of malloc is defined and no more casts are required    Char* str ="Hello world!"; Log_string (str); //print variable names and values for variableslog_pointer (PI); //k is defined within a macro, its scope is very small, and there is no need to defineFOREACH (k,5) BEGIN Pi[k]= k +1; END//k is defined within the macro and is no longer defined here.FOREACH (k,5) BEGINintValue =Pi[k];        Log_int (value);    END free (PI);    Log_pointer (PI); return 0;}

4. Summary

(1) Macros and functions are not a competitive relationship

(2) Macros can accept any type of parameters , high efficiency, error -prone

(3) The parameters of the function must be fixed type , the efficiency is slightly low, not easy error

(4) Macros can implement functions that cannot be implemented by functions

The 46th lesson function and Macro analysis

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.