I just wish C had something like defer in go, That would cover most cases.
Show HN: A simple garbage collector for C
31–40 of 62 posts
Re: Show HN: A simple garbage collector for C
#32I just wish C had something like defer in go, That would cover most cases.
You can "emulate" the defer statement with a Scope Guard[0], or if you're willing to use a macro, you can use the __COUNTER__ macro as an "anonymous" variable.
Re: Show HN: A simple garbage collector for C
#33I just wish C had something like defer in go, That would cover most cases.
But the really classic way of doing "defer" in C is to do what Linux does:
int foo(void)
{
char *ptr = malloc(13);
int error = 0;
error = bar();
if (error
Once you get used to it, it feels very natural to write and understand.Re: Show HN: A simple garbage collector for C
#34Earlier quoted context omitted.
That's exactly what this uses. It just hides it inside a syntax that is more similar to what Go uses.
Ah, apologies, the link wasn't loading at the moment and I assumed it was something else. Regarding its use in the wild, I've encountered it exactly once, in the lastpass-cli (available on github). I personally wouldn't consider using it for one of my projects because it's too nonstandard, however I suppose that for a security-sensitive application it might reduce the likelihood of messing error handling and leaking…
Re: Show HN: A simple garbage collector for C
#35Just out of interest for people not familiar with C, latest goings on there etc, what's the usecase where you'd want a garbage collector in C? My (very surface level) understanding was always the trade off for the increased manual effort of using C - manual memory management being one example - was that you could tailor your solution exactly to your usecase for increased performance / lower resource use. If you're go…
Re: Show HN: A simple garbage collector for C
#36Earlier quoted context omitted.
Regarding having tons of exit points, I find it actually adds no more cognitive load to use only a single return per function, compared to having multiple returns and keeping resource-freeing up-to-date for each. Most of the challenge in writing code with a single return seems to come from the fact that I'm not used to doing it. Of course, sometimes it leads to heavily nested control flow, although I'm not sure yet w…
What you say makes sense but I'd advise beginners reading this comment not to overdo it. I've seen new coders "cargo culting" this one-return-per-function rule and leading to code that was, in my opinion, unnecessarily complicated. For instance parameter validation is one instance where I think an early return is very much warranted. There you typically have no cleanup to do since it's effectively the prelude of the…
Re: Show HN: A simple garbage collector for C
#37Re: Show HN: A simple garbage collector for C
#38I just wish C had something like defer in go, That would cover most cases.
Check out Zig if you’re looking for a C replacement with a go style defer. It also interops about as well with C as C does.
Re: Show HN: A simple garbage collector for C
#39Re: Show HN: A simple garbage collector for C
#40How about efficiency? Looks like the collector has to scan through all allocated memory and the whole stack to check whether there could be a pointer. And if any data looks like such a pointer (even by coincidence) the corresponding memory cannot be collected. Anyway: isn't this just the same concept as the Boehm conservative GC? What's the difference/improvement?
> The focus of gc is to provide a conceptually clean implementation of a mark-and-sweep GC, without delving into the depths of architecture-specific optimization (see e.g. the Boehm GC for such an undertaking). It should be particularly suitable for learning purposes and is open for all kinds of optimization (PRs welcome!).