how to freeing pointers using macro in c

前端 未结 5 637
我寻月下人不归
我寻月下人不归 2021-01-16 06:26

I want to write macro in c code to freeing many pointers like this :

FREE(ptr1, ptr2, ptr3, ptr4, ptrx);

For me, this is better than

5条回答
  •  遥遥无期
    2021-01-16 06:44

    You can pass variable number of arguments in macro. Following code works fine:

    #define FREE_ALL(...) \
    do { \
        int i=0;\
        void *pta[] = {__VA_ARGS__}; \
        for(i=0; i < sizeof(pta)/sizeof(void*); i++) \
        { \
            free(pta[i]); \
        }\
    } while(0)
    

提交回复
热议问题