Live data from Hacker News

Show HN: A simple garbage collector for C

github.com

31–40 of 62 posts

Re: Show HN: A simple garbage collector for C

#31
post #4

I just wish C had something like defer in go, That would cover most cases.

If you're willing to use GCC extensions (available also on clang, so should cover most operating systems, even Windows with clang-cl), there's __attribute__((__cleanup__(fn))), as used for instance by systemd: https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Common-Variable...

Re: Show HN: A simple garbage collector for C

#32
post #4

I just wish C had something like defer in go, That would cover most cases.

This seems like a great place to plug C++ - Destructors are what you want. While I'm personally a fan of C++-as-a-whole, there's nothing stopping you using C-with-classes.

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.

[0] https://github.com/ricab/scope_guard

Re: Show HN: A simple garbage collector for C

#33
post #4

I just wish C had something like defer in go, That would cover most cases.

The primary use-case of defer (cleaning up resources) can be done with __attribute__((__cleanup__())). This effectively allows you to do RAII in C, though it does require some careful thought when copying variables (you need to "move" them a-la Rust).

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

#34
post #30
post #24

Earlier 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…

LXC uses it as well, and they have additional macros to "move" pointers and file descriptors to make sure you don't double-free or similar things.

Re: Show HN: A simple garbage collector for C

#35

Just 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…

I could imagine something like this being applicable towards large legacy codebases with a history of memory leaks and segfaults. With legacy codebases you're often only able to tread water and won't be in a position to track everything to it's root cause.

Re: Show HN: A simple garbage collector for C

#36
post #19

Earlier 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…

Not unreasonable for functions to have guard returns, a 'happy' return. And a goto fail return. That's decent pattern when you have a function that handles modifying state.

Re: Show HN: A simple garbage collector for C

#38
post #4

I 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.

Or D, which integrates with native C libs and has the concept of 'exit scope' for deferred code.

Re: Show HN: A simple garbage collector for C

#39
How 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?

Re: Show HN: A simple garbage collector for C

#40
post #39

How 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?

Second paragraph in README:

> 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!).

Post reply on HN