Live data from Hacker News

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

hwisnu.bearblog.dev

171–180 of 277 posts

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

#172
post #56

Earlier quoted context omitted.

I'd think a POSIX mutex--a standard API that I not only could implement anywhere, but which has already been implemented all over the place--is way more "cross platform" than use of atomics.

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 :-)

[deleted]

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

#173
post #13
post #7

Earlier quoted context omitted.

Yeah, kids like to waste time to make C more safe or bring C++ features. If you need them, use C++ or different language. Those examples make code look ugly and you are right, the corner cases. If you need to cleanup stuff on early return paths, use goto.. Its nothing wrong with it, jump to end when you do all the cleanup and return. Temporary buffers? if they arent big, dont be afraid to use static char buf[64]; No…

Can you share such a corner case?

No, because I did NOT do serious analisis of this. Nor I care, ask upper commenter.. C have some corner case and undefined behaviours and this stuff will make it worse IMO.

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

#174
post #56

Earlier quoted context omitted.

I'd think a POSIX mutex--a standard API that I not only could implement anywhere, but which has already been implemented all over the place--is way more "cross platform" than use of atomics.

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?

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

#176
post #7

Earlier quoted context omitted.

Yeah, kids like to waste time to make C more safe or bring C++ features. If you need them, use C++ or different language. Those examples make code look ugly and you are right, the corner cases. If you need to cleanup stuff on early return paths, use goto.. Its nothing wrong with it, jump to end when you do all the cleanup and return. Temporary buffers? if they arent big, dont be afraid to use static char buf[64]; No…

> static char buf[64]; In a function? That makes the function not-threadsafe and the function itself stateful. There are places, where you want this, but I would refrain from doing that in the general case.

Holy moly.. Thread safety.. Good point and Bad point. I myself use threads sparsly, so I dont intermix calls between threads..

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

#177

Earlier quoted context omitted.

I think these threads attract people that write code for performance-critical use cases which explains the "cosmic horror" over pretty benign things. I agree though: most programs aren't going to be brought to their knees over some GC sweeps every so often.

Outside of hobbyist things, performance-critical code is the only responsible use case for a non-memory safe language like C in 2025, so of course it does. (Even that window is rapidly closing, though; languages like Rust and Swift can be better than C for perf-critical things because of the immutability guarantees.)

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

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

#178
post #158

Earlier quoted context omitted.

A simple pointer ownership model can achieve temporal memory safety, but I think to be convenient to use we may need lifetimes. I see no reason this could not be added to C.

A C with lifetimes would be nice, I agree. Would be awesome if someone did a study to see if it's actually achievable... Cyclone's approach was certainly not enough, and I think some sort of generics or a Hindley-Milner type system might be required to get it to work, otherwise lifetimes would become completely unusable.

Yes, one needs polymorphism. Let's see. I have some ideas.

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

#179
post #175
post #119

Earlier quoted context omitted.

RC is a GC method and the least efficient one.

It's the most predictable and has much less overhead than a moving collector.

Only when we forget about the impact of cycle collections, or domino effects stoping the world when there is a cascade of counters reaching zero.

The optimisatios needed to improve such scenarions, are akin to a poor man's tracing GC implementation.

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

#180
post #94

Earlier quoted context omitted.

Not really, only in the mind of haters.

Let's start with object construction. You think you're creating an object. The compiler thinks you're declaring a function. Widget w(); // I made a widget, right? RIGHT? Wrong. You just declared a function that takes no parameters and returns a Widget. The compiler looks at this line and thinks "Ah yes, clearly this person wants to forward-declare a function in the middle of their function body because that's a compl…

I could write a similar rant based on C and all the dialects that many uneducated on the ways of WG14 assume being proper C, after all I keep track of it since 1991, its UB flaws and security holes that plague C++ due to the base compatibility, but I won't change your mind, nor you will change mine.

By the way, C auto now means the same as C++11 auto.

Post reply on HN