Live data from Hacker News

Defer: Resource cleanup in C with GCCs magic

oshub.org

1–10 of 99 posts

Re: Defer: Resource cleanup in C with GCCs magic

#5
I don't understand why people insist on simulating a poor substitute for RAII with a feature that is itself almost decent RAII.

> If malloc fails and returns NULL, the cleanup function will still be called, and there’s no simple way to add a guard inside free_ptr.

free(NULL) is a no-op, this is a non-issue. I don't know what's so hard about a single if statement anyway even if this were an issue.

Re: Defer: Resource cleanup in C with GCCs magic

#6

I don't understand why people insist on simulating a poor substitute for RAII with a feature that is itself almost decent RAII. > If malloc fails and returns NULL, the cleanup function will still be called, and there’s no simple way to add a guard inside free_ptr. free(NULL) is a no-op, this is a non-issue. I don't know what's so hard about a single if statement anyway even if this were an issue.

`free(NULL);` will crash on some platforms that gcc supports, I believe.

Re: Defer: Resource cleanup in C with GCCs magic

#9
post #6

I don't understand why people insist on simulating a poor substitute for RAII with a feature that is itself almost decent RAII. > If malloc fails and returns NULL, the cleanup function will still be called, and there’s no simple way to add a guard inside free_ptr. free(NULL) is a no-op, this is a non-issue. I don't know what's so hard about a single if statement anyway even if this were an issue.

`free(NULL);` will crash on some platforms that gcc supports, I believe.

It shouldn't https://pubs.opengroup.org/onlinepubs/7908799/xsh/free.html

>If ptr is a null pointer, no action occurs.

Re: Defer: Resource cleanup in C with GCCs magic

#10
post #6

I don't understand why people insist on simulating a poor substitute for RAII with a feature that is itself almost decent RAII. > If malloc fails and returns NULL, the cleanup function will still be called, and there’s no simple way to add a guard inside free_ptr. free(NULL) is a no-op, this is a non-issue. I don't know what's so hard about a single if statement anyway even if this were an issue.

`free(NULL);` will crash on some platforms that gcc supports, I believe.

can we just do `if(*ptr == NULL) return;` ?
Post reply on HN