2015-08-14 8 views
6

cleanup संकलक विशेषता के साथ एक चर प्रारंभ करने का कोई तरीका है? या क्या वे चर घोषित करने के बाद मूल्य निर्धारित करना है?क्लीनअप विशेषता के साथ चर प्रारंभ करने के लिए कैसे?

मैंने नीचे = malloc(10); के सामने = malloc(10); के सामने cleanup विशेषता डालने की कोशिश की है और = 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; 
} 

वहाँ cleanup का उपयोग कर और एक लाइन पर चर आरंभ के लिए एक अलग वाक्य रचना है? या क्या मुझे नीचे दिए गए उदाहरण में वैरिएबल घोषित करने के बाद मान सेट करना होगा?

#include <stdio.h> 
#include <stdlib.h> 

static inline void cleanup_buf(char **buf) 
{ 
    if(*buf == NULL) 
    { 
     return; 
    } 

    printf("Cleaning up\n"); 

    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("malloc\n"); 
     return -1; 
    } 

    return 0; 
} 

उत्तर

3

बस mistypo। बस ...

//#define auto_clean __attribute__((cleanup (cleanup_buf))); 
//              ^
#define auto_clean __attribute__((cleanup (cleanup_buf))) 
संबंधित मुद्दे