Live data from Hacker News

Stupid Smart Pointers in C

blog.kevinalbs.com

171–174 of 174 posts

Re: Stupid Smart Pointers in C

#171

Earlier quoted context omitted.

> Today the libc allocators have advanced by maybe tens of thousands of PhDs worth of theory and practice. For fun I wrote up a very basic memory manager and benchmarked it against free/malloc. On my Mac (M3) the manager was 3x faster. On a random kubernetes pod (alpine) it was 33x faster. Performance increases as memory size goes up.

If this is true, can some of your ideas be integrated into the standard libc allocator?

Well, not on MacOS, I imagine (you tell me which repository/mailing list they should send a PR to :p). Really Windows and MacOS seem like the places where a malloc replacement is generally quite useful (Windows is a particularly big offender in this regard, Windows malloc is abysmal - MacOS less so iirc but still pretty bad) - it's glibc that's somewhat of an outlier by being generally pretty good.

Re: Stupid Smart Pointers in C

#172
post #106

Or rather, given that every relevant C compiler is also a c++ compiler, just compile as c++ and use std::unique_ptr? I love C but I just can't understand the mental gymnastics of people that prefer this kind of hacks compared to just using C++

Unfortunately, that's not true. C is not 100% compatible with C++. There's a whole heap of incompatibilities that you can hit, that will prevent a lot of non-trivial C programs from compiling under C++. Things like character literals being a char in C++ and an int in C. Or C allowing designated initialisers for arrays, but C++ not.

I know, but it's not incredibly hard to work with them to be fair. People have been mixing C and C++ for decades now, we know where the sharp edges are pretty well

Re: Stupid Smart Pointers in C

#173
post #142

Earlier quoted context omitted.

> Use __attribute__((cleanup)). It's available in GCC and Clang Really, don’t do this, it’s a portability nightmare. If you’re going to write C stick to code that easy to run under MSVC.

Running under MSVC is overrated and more trouble than its worth. Clang and mingw-w64 GCC work just fine for targeting Windows.

Disagree. It’s fine and trivially easy. It’s only hard for Linux people who build software for Linux first and only. Then have a shocked pikachu face when they need to run in a different environment.

mingw is radically more problematic than MSVC. Don’t use mingw.

Re: Stupid Smart Pointers in C

#174
post #142

Earlier quoted context omitted.

Running under MSVC is overrated and more trouble than its worth. Clang and mingw-w64 GCC work just fine for targeting Windows.

Running under MSVC implies I'm building on Windows. In reality I'm cross compiling with Clang.

It’s a shame that glibc is so badly designed that cross-compiling for Linux is only vaguely possible because Andrew Kelley moved mountains via Zig.
Post reply on HN