如何使用cleanup属性初始化变量?
是否有方法使用如何使用cleanup属性初始化变量?,c,C,是否有方法使用cleanup编译器属性初始化变量?或者我必须在声明变量后设置值吗 我尝试将cleanup属性放在=malloc(10)前面如下面和后面的示例所示=malloc(10)但两者都不编译 #include <stdio.h> #include <stdlib.h> static inline void cleanup_buf(char **buf) { if(*buf == NULL) { return; }
cleanup
编译器属性初始化变量?或者我必须在声明变量后设置值吗
我尝试将cleanup
属性放在=malloc(10)前面代码>如下面和后面的示例所示=malloc(10)代码>但两者都不编译
#include <stdio.h>
#include <stdlib.h>
static inline void cleanup_buf(char **buf)
{
if(*buf == NULL)
{
return;
}
printf("Cleaning up\n");
free(*buf);
}
#define auto_clean __attribute__((cleanup (cleanup_buf)));
int main(void)
{
char *buf auto_clean = malloc(10);
if(buf == NULL)
{
printf("malloc\n");
return -1;
}
return 0;
}
只是米斯蒂波。只是
//#define auto_clean __attribute__((cleanup (cleanup_buf)));
// ^
#define auto_clean __attribute__((cleanup (cleanup_buf)))
//#define auto_clean __attribute__((cleanup (cleanup_buf)));
// ^
#define auto_clean __attribute__((cleanup (cleanup_buf)))