Live data from Hacker News

Giving C a superpower: custom header file (safe_c.h)

hwisnu.bearblog.dev

221–230 of 277 posts

Re: Giving C a superpower: custom header file (safe_c.h)

#221

Intentionally or not, this post demonstrates one of the things that makes safer abstractions in C less desirable: the shared pointer implementation uses a POSIX mutex, which means it’s (1) not cross platform, and (2) pays the mutex overhead even in provably single-threaded contexts. In other words, it’s not a zero-cost abstraction. C++’s shared pointer has the same problem; Rust avoids it by having two types (Rc and…

ISO C has had mutexes since C11 I think.

In any case, you could use the provided primitives to wrap the C11 mutex, or any other mutex.

With some clever #ifdef, you can probably have a single or multithreaded build switch at compile time which makes all the mutex stuff do nothing.

Re: Giving C a superpower: custom header file (safe_c.h)

#222

Earlier quoted context omitted.

To lift things up a level: I think a language’s abstractions have failed if we even need to have a conversation around what “cross platform” really means :-)

If that's the bar, what language's abstractions haven't failed?

wasm and lambda calculus

Re: Giving C a superpower: custom header file (safe_c.h)

#223

Earlier quoted context omitted.

> Because about 99% of the time the garbage collect is a negligible portion of your runtime lol .. reality disagrees with you. https://people.cs.umass.edu/~emery/pubs/gcvsmalloc.pdf#:~:te... On page 3 they broadly conclude that if you use FIVE TIMES as much memory as your program would if managed manually, you get a 9% performance hit. If you only use DOUBLE, you get as much as a 70% hit. Further on, there are compre…

Methodology seems kind of dubious: > We introduce a novel experimental methodology that lets us quan- tify the performance of precise garbage collection versus explicit memory management. Our system allows us to treat unaltered Java programs as if they used explicit memory management by relying on oracles to insert calls to free. These oracles are generated from profile information gathered in earlier application run…

What specifically seems dubious about that? I thought it was quite a clever idea.

Re: Giving C a superpower: custom header file (safe_c.h)

#224
post #52
post #35

Earlier quoted context omitted.

Why would I want to run a garbage collector and deal with it's performance penalties?

Because about 99% of the time the garbage collect is a negligible portion of your runtime at the benefit of a huge dollop of safety. People really need to stop acting like a garbage collector is some sort of cosmic horror that automatically takes you back to 1980s performance or something. The cases where they are unsuitable are a minority, and a rather small one at that. If you happen to live in that minority, great…

The Java stop-the-world garbage collector circa the late 90s/early 2000s traumatized so many people on automated garbage collection.

Re: Giving C a superpower: custom header file (safe_c.h)

#225
post #182

Earlier quoted context omitted.

Productivity, portability, stability, mind-share, direct access to OS APIs... there's a lot of reasons to still use C.

Only if the OS is written in C, and has its APIs exposed as C APIs to userspace. Quite a few OSes don't fit that rule.

Could you name two of these that are important to you?

Re: Giving C a superpower: custom header file (safe_c.h)

#226

Earlier quoted context omitted.

I think they’re one and the same: C++ doesn’t have program-level thread safety by construction, so primitives like shared pointers need to be defensive by default instead of letting the user pick the right properties for their use case. Edit: in other words C++ could provide an equivalent of Rc, but we’d see no end of people complaining when they shoot themselves in the foot with it. (This is what “zero cost abstract…

> C++ doesn’t have program-level thread safety by construction It does. It’s called a process. Everyone chose convenience and micro-benchmarks by choosing threads instead.

"Thread truther" is not one of the arguments I had on the bingo card for this conversation.

Re: Giving C a superpower: custom header file (safe_c.h)

#227
post #35
post #9

A recent superpower was added by Fil aka the pizlonator who made C more Fil-C with FUGC, a garbage collector with minimal adjustments to existing code, turning it into a memory safe implementation of the C and C++ programming languages you already know and love. https://news.ycombinator.com/item?id=45133938 https://fil-c.org/

Why would I want to run a garbage collector and deal with it's performance penalties?

Because C is very unsafe, but there are still many billions of lines of C in use, so making C safer is a great idea.

Re: Giving C a superpower: custom header file (safe_c.h)

#228
post #182

Earlier quoted context omitted.

Only if the OS is written in C, and has its APIs exposed as C APIs to userspace. Quite a few OSes don't fit that rule.

Could you name two of these that are important to you?

Or even one. I know there are operating systems in use that are not written in C, but the major ones are written in C. And anyways, it's not just the OS. There's a pile of C code. Fil-C is a fantastic idea. I think Fil is going to make it good enough to use in production, and I badly want to use it in production.

Re: Giving C a superpower: custom header file (safe_c.h)

#229

Earlier quoted context omitted.

Methodology seems kind of dubious: > We introduce a novel experimental methodology that lets us quan- tify the performance of precise garbage collection versus explicit memory management. Our system allows us to treat unaltered Java programs as if they used explicit memory management by relying on oracles to insert calls to free. These oracles are generated from profile information gathered in earlier application run…

What specifically seems dubious about that? I thought it was quite a clever idea.

If you dig into the paper, on page 3 they find out that their null oracle approach (ie without actually freeing the memory) increases run times erratically by 12 to 33%. They then mention that their simulated approach should handle that case but it seems unlikely to me that their stats aren't affected. Also they disable multi-threading – again for repeatability – but that will obviously have a performance impact.

Re: Giving C a superpower: custom header file (safe_c.h)

#230

This feels like a misrepresentation of features that actually matter for memory safety. Automatically freeing locals and bounds checking is unquestionably good, but it's only the very beginning. The real problems start when you need to manage memory lifetimes across the whole program, not locally. Can you return `UniquePtr` from a function? Can you store a copy of `SharedPtr` somewhere without accidentally forgetting…

> Can you return `UniquePtr` from a function?

Yes: you can return structures by value in C (and also pass them by value).

> Can you store a copy of `SharedPtr` somewhere without accidentally forgetting to increment the refcount?

No, this you can't do.

Post reply on HN