2 ms·
C: char *buf __attribute__ ((__cleanup__(free_buf))) = malloc(1024); D: char* buf = cast(char*)malloc(1024); scope (exit) free(buf); https://dla
by WalterBright 4y ago
C:
char *buf __attribute__ ((__cleanup__(free_buf))) = malloc(1024);
D:
char* buf = cast(char*)malloc(1024);
scope (exit) free(buf);
https://dlang.org/spec/statement.html#scope-guard-statement https://dlang.org/spec/statement.html#scope-guard-statement
The rationale for it:
https://dlang.org/articles/exception-safe.html https://dlang.org/articles/exception-safe.html
The idea for this came from Andrei Alexandrescu and Petru Marginean who proposed it for C++:
http://drdobbs.com/184403758 http://drdobbs.com/184403758