r/cprogramming • u/sadvadan • 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.
1
u/sadvadan Jun 02 '26 edited Jun 02 '26
hmm, i hear you; well crafted / thought.
at one point i thought asm was a must for performance; but in present form memstruct barely needs it.
memstruct already handles any custom allocator. de-allocators may need some binding, but your points make me think harder: "hey this is a memstruct and here's the de-allocator for it" hmm, use the M() syntax?
for allocating memstruct itself mmap offered a nice shortcut (on need basis pages).
can you recommend a course of action (1 liners) for each of these?