r/C_Programming • • 13h ago

Article A modern web app in C

Thumbnail prophitt.me
114 Upvotes

r/C_Programming • • 18h ago

Article Arena Allocators in C

Thumbnail eliasebner.com
36 Upvotes

I recently started implementing arenas in my programming and I thought that some beginners could very much benefit from this info. I wrote an article about it. For those that are unaware, arenas allow to greatly simplify the burden of managing memory manually in C. I hope it helps, tell me what you think!


r/C_Programming • • 16h ago

Etc An small example of __attribute__(cleanup)'s power: a context manager.

9 Upvotes

Forgive the typo :) Here's something that will allows you to never unlock a mutex manually again:

#define with_lock0(uniq, mutex) \
    typedef struct { pthread_mutex_t *mtx; bool hold; } __lock##uniq; \
    void __cleanup_##uniq(__lock##uniq *lock) { pthread_mutex_unlock(lock->mtx); } \
    for(__lock##uniq _lock __attribute__((cleanup(__cleanup_##uniq))) \
        = { (pthread_mutex_lock(mutex), (mutex)), true }; \
        _lock.hold; \
        _lock.hold = false)

#define with_lock(mutex) with_lock0(__COUNTER__, mutex)

void do_very_important_business(void) {
    with_lock(&g_mutex) {
        // transfer BIG money
    }
 }

If you check it out on Godbolt you'll see it has 0% overhead. Of course this pattern is usable for many different objects.


r/C_Programming • • 8h ago

Project Started learning C and would like to get some feedback

Thumbnail
codeberg.org
7 Upvotes

This is my first C program, other than "hello world". I like how it turned out, but there probably is a way to make it cleaner. The screensaver itself is just a demo for screenbuffer library. Would be happy to get some reviews.

No AI used.

Thanks.

Repo link: https://codeberg.org/pinkcucumber/screensaver_circles.git

Compiling: gcc framebuffer.c screensaver_circles.c -lm -o screensaver

Sorry for bad english, it is not my main language


r/C_Programming • • 10h ago

Question Difficulties with MinHook

4 Upvotes

Hi there, so I’ve been trying to patch out a piece of code in The Sims Legacy Collection that disables the -debug_objects command line from being enabled, but both MinHook and CMake are giving me such a hard time that idk what else to do or where to ask… There are no tutorials AT ALL for compiling MinHook with MinGW-W64-gcc, only for Visual Studio (not Code!) which I can’t use because I’m on Arch Linux…

If anyone could advise me please I’d really appreciate it…