小编Rem*_*emy的帖子

Free不会释放作为va_arg列表发送的指针

我想用下面的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)

c linux variadic-functions

2
推荐指数
1
解决办法
106
查看次数

错误:对于从字符串文字中选择的字符,大小写标签没有减少为整数常量

我用gcc 8.2编译此代码时收到此错误:

#define literal "string"
switch(i) {
case literal[0]:
break;
}
Run Code Online (Sandbox Code Playgroud)

在我的示例中,编译器能否将表达式literal [0]减少为“ s”?

c gcc

1
推荐指数
1
解决办法
133
查看次数

标签 统计

c ×2

gcc ×1

linux ×1

variadic-functions ×1