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.

8 Upvotes

21 comments sorted by

•

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.

1

u/og_hylyx Aug 30 '26

Largely in part yes, it is very redundant

0

u/lehmagavan Aug 28 '26

Thanks for pointing out. As for the shptr_ISNULL itself, I'll keep it because I like how it fits with the rest of the API.

2

u/Lyraele Aug 28 '26

Idiomatic C would not compare to NULL, generally you just use x or !x rather than (x != NULL) of (x == NULL). Keep it terse.

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.

2

u/mikeblas Aug 27 '26

Thank you for your disclosure. I have approved your post.

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.

2

u/aocregacc Aug 28 '26

why would an extra level of indirection be required?

1

u/lehmagavan Aug 29 '26

You're right, what I mean is that Im not sure if I want to complicate it with something that doesn't seem that usefull or neccessery to me.

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!

1

u/VegetableMiserable54 Aug 29 '26

Yes they will force you to use AI... Nevertheless, AI generated C code, requires high-level C knowledge to understand it. So keep building.

1

u/centuryx476 Aug 30 '26

Or just use C++

-9

u/chrism239 Aug 27 '26

This is a C subreddit, not a C++ one.

13

u/EpochVanquisher Aug 27 '26

The code is C, it’s just not explained well

4

u/Elect_SaturnMutex Aug 28 '26

Shared pointers are implemented in openssl too, which is entirely in C. Well some ASM and perl too.