5 ms·
__attribute__((cleanup)) is ugly because for some reason, the gcc folks decided that the parameter passed to cleanup needed to be a function pointer of signatur
by 10000truths 5y ago
__attribute__((cleanup)) is ugly because for some reason, the gcc folks decided that the parameter passed to cleanup needed to be a function pointer of signature void(void**) instead of the much more sane void(void*), with no way to allow implicit casting of the parameter to a function pointer of different type, so none of the basic cleanup functions (e.g. free, fclose) work with it out of the box - you need to pollute your codebase with one wrapper for each cleanup function that you want to use.
They should have made it so that cleanup took an expression block:
void* foo __attribute__((cleanup({ free(foo); }))) = malloc(bar);
Or at least allow a statement expression returning a compile-time known function pointer:
void* foo __attribute__((cleanup(({ void wrapper(void** p) { free(*p); }; wrapper })))) = malloc(bar);
Sure, it still looks ugly, but at least a single generic macro would be able to clean it up and make it look better.
- kaba0 5y agoOh my God, why do we put up with C at all?
- gmfawcett 5y agoWhat are you going to do about it? I suppose you could delete all the C software from your system. Or make a list of all the C programs that your system depends on, and persuade each of their maintainers to change languages. Or you could just put up with C, like everybody else does, and be quietly thankful that the myriad layers of our modern, complex systems are being maintained by other people.
- rwmj 5y agoSure - I agree! But, it exists and it's widely used already. The C working group should concentrate on standardizing existing practice and not start striking out on unproven and weird new syntaxes and keywords.
- deleted 5y ago[deleted]
- eqvinox 5y agoThe "good" way to go about that would be to have a second "cleanup_value" attribute. (Or, since the standard would be creating new names, have "cleanup" and "cleanup_ptr" instead. Assuming that this is a separate attribute namespace, which I believe it is[?]) FWIW, you do sometimes need the address of the variable. Particularly if it's some struct that is made a member of some container (e.g. linked list) temporarily - you need the original variable's address to unlink it.