logoalt Hacker News

vbezhenaryesterday at 10:41 AM2 repliesview on HN

One small nitpick: you don't need check before `free` call, using `free(NULL)` is fine.


Replies

t43562yesterday at 11:26 AM

You're right that it's not needed in my example but sometimes the thing that you're freeing has pointers inside it which themselves have to be freed first and in that case you need the if.

There are several other issues I haven't shown like what happens if you need to free something only when the return code is "FALSE" indicating that something failed.

This is not as nice as defer but up till now it was a comparatively nice way to deal with those functions which were really large and complicated and had many exit points.

show 1 reply
jagged-chiselyesterday at 10:47 AM

But it does keep one in the habit of using NULL checks.

show 1 reply