Rem*_*emy 2 c linux variadic-functions
我想用下面的freeS宏释放一个变量指针列表:freeS(s1,s2,...);
尽管从freeSF函数获取了第一个指针地址的打印,但我的代码并没有释放第一个指针.在主要的,免费(s1)工作,但它应该.正如预期的那样在主要崩溃中自由(s2).
如何释放freeSF函数中的s1指针?
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#define freeS(...) freeSF("", __VA_ARGS__, NULL)
void freeSF(char *paramType, ...) {
va_list pl;
va_start(pl, paramType);
paramType = va_arg(pl, char *);
while (paramType) {
printf("arg %p\n", paramType);
free(paramType);
paramType = va_arg(pl, char *);
}
va_end(pl);
}
int main(int ARGC, char** ARGV) {
char *s1 = strdup("1");
char *s2 = strdup("2");
printf("s1 %p, s2 %p\n", s1, s2);
freeS(s1, s2);
free(s1);
}
Run Code Online (Sandbox Code Playgroud)