Live data from Hacker News

Show HN: A simple garbage collector for C

github.com

21–30 of 62 posts

Re: Show HN: A simple garbage collector for C

#21
post #12
post #7

Earlier quoted context omitted.

That link seems to be broken? Nothing opens when I click it.

http://fdiv.net/2015/10/08/emulating-defer-c-clang-or-gccblo... The original link works.

The original link pops up with a HTTP basic authentication request for me, hence the use of archive.

Re: Show HN: A simple garbage collector for C

#22
post #5
post #4

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

Defer can be emulated with the block extensions of Clang and GCC [0], though I'm not sure how much I'd like to see something like that in a codebase. [0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...

There's also __attribute__ ((__cleanup__())) which can be used to implement RAII in C.

Re: Show HN: A simple garbage collector for C

#23
post #18
post #4

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

I know that's not directly what you're asking for, but I found the other day that "C++ Core Guidelines" have a helper library "GSL_util" that provide something similar to Go's defer. The implementation is very simple, it's just using a class for its destructor: template class final_act { public: explicit final_act(F f) noexcept : f_(std::move(f)), invoke_(true) {} final_act(final_act&& other) noexcept : f_(std::move(…

Of course, in C++ the need for such constructs is reduced due to RAII and scoping running destructors automatically. This is useful when you need to interact with C libraries that you don’t want to wrap in C++ (and when I was doing something like this in the past, I rolled my own as well).

Re: Show HN: A simple garbage collector for C

#24
post #22
post #5

Earlier quoted context omitted.

Defer can be emulated with the block extensions of Clang and GCC [0], though I'm not sure how much I'd like to see something like that in a codebase. [0] https://web.archive.org/web/20180426195701/http://fdiv.net/2...

There's also __attribute__ ((__cleanup__( ))) which can be used to implement RAII in C.

That's exactly what this uses. It just hides it inside a syntax that is more similar to what Go uses.

Re: Show HN: A simple garbage collector for C

#26
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 going for a garbage collector, why not also benefit from some of the increased language power/features of a higher level language?

Re: Show HN: A simple garbage collector for C

#28

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…

As the author mentioned in the README, it's going to be used for the LISP interpreter he/she is writing from scratch.

Re: Show HN: A simple garbage collector for C

#29

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…

The usecase for this project (according to the README) is:

> The original motivation for gc is my desire to write my own LISP in C, entirely from scratch - and that required garbage collection.

Another reason would be in platforms where you cannot run the language of your choice (because lack of implementations of JVM / Python / whatever), although those would maybe not allow malloc either.

Re: Show HN: A simple garbage collector for C

#30
post #24
post #22

Earlier quoted context omitted.

There's also __attribute__ ((__cleanup__( ))) which can be used to implement RAII in C.

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 things all over the place. That being said using non-standard features might also make it more likely for a contributor to misunderstand what the code does precisely and introduce a problem.

Post reply on HN