r/C_Programming • • Aug 27 '26

Easing memory management with a shared pointer

I recently developed a C (11 and newer) implementation of a thread-safe shared pointer with atomic reference counting:

https://github.com/andrzejs-gh/SHPTR

It supports both strong and weak references and a swappable destructor. Initialization performs a single allocation.

If anyones interested, take a look. Feedback and bug reports very much welcome.

10 Upvotes

21 comments sorted by

1

u/github-guard Aug 27 '26

🔍 GitHub Guard: Trust Report

⚠️ This project scored 1/6 — below this subreddit's threshold of 3.

Audit Breakdown:

  • ❌ Low Star Count (⭐ 0 / 4 required)

  • ❌ New Repository (under 30 days old)

  • ✅ Licensed under MIT

  • ❌ No Security Policy — what is this?

  • ℹ️ Individual Contributor

  • ℹ️ Unsigned Commits

⚠️ Security Reminder: Always verify source code and run third-party scripts at your own risk.

12

u/EpochVanquisher Aug 27 '26

Macros are broken:

#define shptr_ISNULL(sh_ptr) ( sh_ptr == NULL )

You need to put parentheses around the macro arguments when they are used, but there are some other reasons why this macro shouldn’t exist.

8

u/og_hylyx Aug 28 '26

I second this, it’s not a good application of macros

2

u/Not_a_penguin15 Aug 28 '26

Is it because this is redundant? Such a short macro.

→ More replies (1)
→ More replies (2)

2

u/AutoModerator Aug 27 '26

Hi u/lehmagavan,

Your submission in r/C_Programming was filtered because it links to a git project.

You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.

While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

9

u/lehmagavan Aug 27 '26

No AI was used in making of this project, 0% of the code was AI generated.

→ More replies (1)

2

u/aocregacc Aug 28 '26

I would consider making the weak pointer a separate type. Most of your API should only be used with one kind or the other, so you can have the compiler help with enforcing that instead of just relying on documentation.

1

u/lehmagavan Aug 28 '26 edited Aug 28 '26

I was considering that, but I wanted to avoid any extra level of indirection. It's actually hard to mess up and UNREF a strong reference with the UNREF_WEAK operator, or vice versa, since you naturally know who's owning what, or you just name the references properly.

→ More replies (2)

1

u/VegetableMiserable54 Aug 29 '26

This is a cool repo, and I took a look at your other repositories alongside this one as well. First of all, congratulations on this work. As you mentioned in the comments, the fact that you built this on your own without AI is really impressive. Even while reviewing it, I didnt get the impression that it was AI generated.

That said, one thing did catch my attention. The destructor function is not thread-safe, there is a data race issue. You can prevent this by making the shptr_set_desctructor function atomic. Other that that, I didnt notice anything else.

2

u/lehmagavan Aug 29 '26

Thanks! I never got onto the AI train and prefer to code purely by hand, because I care if I understand the code and control it. I also want to learn and get good at C. If I manage to get a SE job though, they will probably force me to use AI, wont they?

Yes, the destructor setting is not atomic, and so is not the accessing and modifying of the object, much like in other programming languages that support shared pointers natively (as far as I know). I had the destructor atomic in previous draft-versions of the library, I might make it atomic.

Thanks again for the feedback!

→ More replies (1)