c/c++语言开发共享如何用cleanup属性初始化变量?

有没有办法用cleanup编译器属性初始化变量? 或者我必须在声明变量后设置值?

我已经尝试将cleanup属性放在= malloc(10);前面= malloc(10); 比如下面的示例和后面= malloc(10); 但是没有编译。

 #include  #include  static inline void cleanup_buf(char **buf) { if(*buf == NULL) { return; } printf("Cleaning upn"); free(*buf); } #define auto_clean __attribute__((cleanup (cleanup_buf))); int main(void) { char *buf auto_clean = malloc(10); if(buf == NULL) { printf("mallocn"); return -1; } return 0; } 

在一行上使用cleanup和初始化变量有不同的语法吗? 或者我必须在声明变量后设置值,如下例所示?

 #include  #include  static inline void cleanup_buf(char **buf) { if(*buf == NULL) { return; } printf("Cleaning upn"); free(*buf); } /* Use this attribute for variables that we want to automatically cleanup. */ #define auto_clean __attribute__((cleanup (cleanup_buf))); int main(void) { char *buf auto_clean; buf = malloc(10); if(buf == NULL) { printf("mallocn"); return -1; } return 0; } 

    只是迷雾。 只是…

     //#define auto_clean __attribute__((cleanup (cleanup_buf))); // ^ #define auto_clean __attribute__((cleanup (cleanup_buf))) 

      以上就是c/c++开发分享如何用cleanup属性初始化变量?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/545017.html

      (0)
      上一篇 2021年1月9日
      下一篇 2021年1月9日

      精彩推荐