Comment by shakna
14 days ago
Attributes, mostly. Which have become so common that defer is very likely to be in the next C standard. [0]
bool function_with_cleanup(void) {
// Allocate first resource
int* buffer1 __attribute__((__cleanup__(free))) = malloc(sizeof(int) * 100);
if(!buffer1) { return false; }
// Allocate second resource
int* buffer2 __attribute__((__cleanup__(free))) = malloc(sizeof(int) * 200);
if(!buffer2) { return false; }
// Open a file
FILE* file __attribute__((__cleanup__(fclose))) = fopen("data.txt", "r");
if (!file) { return false; }
return true;
}
[0] https://thephd.dev/c2y-the-defer-technical-specification-its...
No comments yet
Contribute on Hacker News ↗