Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

FWIW, gcc does have destructors for stack-allocated data as an extension: http://gcc.gnu.org/onlinedocs/gcc/Variable-Attributes.html#i...

    #include <stdio.h>
    
    typedef struct foo foo_t;
    
    void foo_cleanup(foo_t *f);
    
    struct foo { int i; };
    
    void foo_cleanup(foo_t *f) {
        printf("f %d\n", f->i);
    }
    
    int main() {
    
        foo_t a __attribute__((cleanup(foo_cleanup))) = { 7 };
    
        printf("l %d\n", __LINE__);
    
        {
            foo_t b __attribute__((cleanup(foo_cleanup))) = { 8 };
            
            printf("l %d\n", __LINE__);
        }
    
        printf("l %d\n", __LINE__);
    }


    l 18
    l 23
    f 8
    l 26
    f 7


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: