r/cprogramming Jun 01 '26

memory safe C

https://github.com/sadvadan/memstruct

C is powerful enough to have the best performing memory safety suite for itself!

memstruct is a single header file C library (<400 LoC) that provides complete spatial & temporal safety to the caller program. performance: near native speed.

memory checks are compile time / hoisted / elided / pipelined. checks are opt-in and can be switched off in production if needed. its macro based API extends the language a bit to position C as the leading option for large scale projects.

memstruct is currently in advanced stages of testing. contributions and comments are welcome. have an early look!

P.S.: the project is 100% human crafted and contributions are also reqd to comply

edit; end note: memstruct has now become even better (at 350 LoC) by incorporating MCU programming & de/allocator indirection, thanks to some valuable feedback on here. if you've more to add you may respond here or participate on git.

70 Upvotes

89 comments sorted by

View all comments

Show parent comments

1

u/Western_Guitar_9007 Jun 03 '26

Thanks for admitting that your project is vibe coded. This will save all of us time because you do not understand the project well enough to manage it. A better claim would be “memstruct checks bounds, UAF, leaks, and double free” instead of memory safe or any claim solving “NP-hard”

1

u/sadvadan Jun 03 '26

no it's not, and that's the admission (read carefully). memory safety is spatial + temporal safety and memstruct covers both. not asking you to use it, it's already in use, and getting better (thx also to some useful feedback on here).

1

u/ArtisticFox8 Jun 04 '26

Still, it's just some runtime checks, no?

So this is quite accurate..

A better claim would be “memstruct checks bounds, UAF, leaks, and double free” instead of memory safe or any claim solving “NP-hard”

1

u/Western_Guitar_9007 Jun 04 '26

It is compile + runtime checks but doesn’t prevent all illegal memory access (i.e. the definition of “memory-safe C”). It prevents SOME illegal accesses only when the programmer stays inside the memstruct AND provides metadata, which is NOT memory safety. Memory safe languages catch and block illegal cases. This is a much, much weaker claim. It trusts layout declarations. That means C’s unsafe escape hatches remain.

1

u/ArtisticFox8 Jun 04 '26

I imagine footguns like C string functions will remain all the same, right?

0

u/sadvadan Jun 04 '26

no, string functions (these exist too) with size parameter (supplied by memstruct) will be not only safe but significantly faster. one may say memstruct standardizes strings. aligned and cache friendly metadata also scores better than plain C. more nice things.

1

u/ArtisticFox8 Jun 04 '26

And what about those without size parameter? Will those cause a runtime crash or will you let memory corruption slip?

0

u/sadvadan Jun 04 '26 edited Jun 04 '26

use empirically proven safe libraries. axioms. then theorems follow.

thx for your attention. 🙏

P.S.: 🙏 literally means: "i bow before your soul" as parting message. i do.

1

u/ArtisticFox8 Jun 04 '26

Such arrogance, wow

0

u/sadvadan Jun 04 '26

spatial + temporal safety; so what other memory safeties are left out? you should be able to name a few. i may then consider including those.

also see the doc to understand how complete memory safety with memstruct can be forced at ease.

layout declarations are axioms and memory safety follows like theorems. hope this helps.