Macro function of C language learning

Macro function: 

 #define 宏名(a,b,c,...) a+b*c

    A macro function is not a real function, but a macro with parameters, just used like a function.

    When using a macro function in the code, there will be two substitutions during preprocessing. The first time the macro function is replaced with a string of codes and expressions behind it, and the second time the parameters in the macro function are replaced into the expression.

    If the code behind the macro function has multiple lines, you can use curly braces to protect it.

    #define macro name(a,b,c,...) {code 1; code 2; ...}

    The code behind the macro function cannot be wrapped directly. If the code is too long, you can use the continuation character to break the line.

    #define macro name(a,b,c,...) { \

        code 1; \

        code 2; \

        ...    \

}

Advantages and disadvantages of ordinary functions and macro functions:

Advantages of macro functions:

            1. The execution speed is fast, it is not a real function call, but a code replacement, and it will not go through parameter passing, jumping,

            return value.

            2. The type of parameters will not be checked, so it is highly versatile.

        Disadvantages of macro functions:

            1. Because it is not a real function call, but a code replacement, every time it is used, a copy of code will be replaced, which will cause code redundancy, slow compilation speed, and larger executable files.

            2. There is no return value, and there can be at most one execution result.

            3. The type check is not strict and the security is low.

            4. Recursive calls cannot be made.

        Advantages of functions:

            1. There is no code redundancy. The code of the function will only be stored in the code segment. When used, it will jump to the past for execution, and return after the execution is completed. You can also add a return value.

            2. High security, type checking will be performed on parameters.

            3. Recursive calls can be made to implement the divide and conquer algorithm.

        Disadvantages of functions:

            1. Compared with the macro function, its execution speed is slower. When calling, it will go through the process of passing parameters, jumping, returning, etc. This process consumes a lot of time.

            2. Type-specific, what type of formal parameter, and what type of actual parameter must be, which cannot be used universally.

How to use the macro function:

What kind of code is suitable for encapsulation into a macro function?

            1. The amount of code is small, even if it is used multiple times, it will not cause excessive redundancy in the code segment.

            2. The number of calls is high, but the number of executions is high.

            3. There is no requirement for the return value.

    Things to keep in mind when designing macros:

        1. Do not add a semicolon at the end.

        2. Add parentheses to prevent ambiguity.

        3. Do not use self-incrementing and self-decrementing variables to provide parameters to macro functions.

Encapsulate a malloc, free function

        my_malloc It needs to record the calling location of my_malloc and the memory address requested

        my_free It needs to record the calling location of my_free and the memory address to be released


    void* _my_malloc(size_t size,const char* file,const char* func,size_t line)
    {
        void* ptr = malloc(size);
        printf("%s %s %d 申请了%d字节的内存,地址是%p\n",file,func,line,size,ptr);
        return ptr;
    }
    // 由于my_malloc必须有返回值,所以只能使用这种方式中转一下
    #define my_malloc(size) _my_malloc(size,__FILE__,__func__,__LINE__)

    #define my_free(ptr) {\
        free(ptr);\
        printf("%s %s %d 释放了内存 %p\n",__FILE__,__func__,__LINE__,ptr);\
    }

Guess you like

Origin blog.csdn.net/m0_62480610/article/details/125927958