I

I built type-safe dynamic arrays for C after struggling with stb_ds.h

Hacker News

I built type-safe dynamic arrays for C after struggling with stb_ds.h

C doesn't come with dynamic data structures like vectors or hashmaps, so developers have to either develop their own on top of arrays and structs, or use an external library. One of the most popular data structure libraries is stb_ds.h. It's a very easy to use single-header library that feels almost like Javascript arrays, but I was getting frustrated with its design. First, iteration relies on index calculations, which is a slower technique than pointer comparison (that's what std::vector uses). Second, the structure hides its array header behind the data pointer. This is done to allow for the square brackets syntax [ ], but it turns debugging into a horrible experience since we can't easily access the header, and this technique relies on undefined behavior which is unreliable, especially when using less popular compilers on different systems. Third, stb_ds.h doesn't do bounds checking, so it silently corrupts memory out-of-bounds access. This is the worst possible way to manage errors, but C is limited on that department. Fourth, stb_ds.h isn't fully type-safe. It relies on sizeof (a) for type-checking, but it's possible to pass different pointer types (char , double ), different primitives of the same size (int, float), or different structs of the same size (struct { node next; }, struct { float x; float y; }) to violate the type contract. The user must enforce type-safety themselves. Fifth, I personally do not like its licensing because MIT forbids re-licensing, and public domain is ambiguous internationally. So I decided to make my own single-header vector library to fix all of those issues. My vector library (vector.h) uses the same iteration technique as std::vector (3 pointers, one for the beginning of the array/elements, one for past the last element, and one past the last valid memory index). This results in faster iteration (which is the most common array usage), clearer debugging with all values being in the struct, and no undefined behavior surprises. vector.h also panics on errors instead of silently corrupting memory. This may seem like a bad design decision, but lots of popular languages crash on out-of-bounds access (Rust, Go, GDScript, etc.). It's almost always a severe bug, and it's best to let the developer know by failing fast. But for those who cannot afford a crash, I implemented the flag VECTOR_NO_PANIC_ON_OOB that turns out-of-bounds access into no-ops. The type-safety requirement was the trickiest to solve, but I landed on macro-generated functions. This is similar to what the Linux kernel sometimes uses like `list.h`, but Linux compiles with gnu89 which includes typeof(), while my vector is strict c89 ISO compliant, which doesn't have typeof(). Finally, for licensing, I chose the BSD Zero-Clause license which allows for re-licensing, is internationally unambiguous, and doesn't require attribution. To make my library production ready, I included a robust test routine. I'd love feedback from C developers who've struggled with similar issues. Code and benchmarks at https://github.com/RolandMarchand/vector.h

Share card

Actual performance

1points
Did not reach leaderboard

Launch Intel predictions

Analyze your own launch →
Indie HackersFits the IH revenue-focused audience · Missing: supports, reddit linkedin, podcasting
91%91% predicted probability of success on Indie Hackers, based on ML models trained on real launch data.
best fitHighest predicted score across all platforms for this description.
Product HuntOn track for Day 1 leaderboard · Strong signals: mac, user, single · Missing: agents, macos, agent
80%80% predicted probability of success on Product Hunt, based on ML models trained on real launch data.
TrustMRRFits verified-revenue profile · Strong signals: personal, way · Missing: mobile apps, ios, entrepreneurs
58%58% predicted probability of success on TrustMRR, based on ML models trained on real launch data.
Hacker NewsStrong engagement from HN community · Strong signals: ide, io · Missing: https docs, excited, just released
52%52% predicted probability of success on Hacker News, based on ML models trained on real launch data.
nativeThis product was originally launched on this platform.
AppSumoMay struggle as an AppSumo deal · Missing: plus, platform, intuitive
46%46% predicted probability of success on AppSumo, based on ML models trained on real launch data.
Acquire.comPre-revenue stage for this audience · Strong signals: arr · Missing: mrr, revenue, profit
12%12% predicted probability of success on Acquire.com, based on ML models trained on real launch data.
BetaListMay not resonate with beta-testers · Missing: web3, chat, crypto
0%0% predicted probability of success on BetaList, based on ML models trained on real launch data.

Incorrect prediction on native model

Similar products

I
I made type-safe Firestore ORM and call it Typesaurus52%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

I made type-safe Firestore ORM and call it Typesaurus

Hacker News3
C8
C8x type safe K8s deployments42%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

C8x type safe K8s deployments

Hacker News2
Ne
Newzly-phantom – Asynchronous type-safe Scala DSL for Cassandra49%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Newzly-phantom – Asynchronous type-safe Scala DSL for Cassandra

Hacker News16
We
We built a type-safe Python ORM for RedisGraph/FalkorDB54%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

We built a type-safe Python ORM for RedisGraph/FalkorDB

Hacker News5
NextStd
NextStd51%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Type-Safe alternative to stdio.h for C

Product Hunt+1
Pr
Promises – Type safe promise library for go41%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Promises – Type safe promise library for go

Hacker News7
Ty
Type-safe metaprogramming for Java (and other killer lang features)29%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Type-safe metaprogramming for Java (and other killer lang features)

Hacker News3
Ty
Type-Safe and Promisified API for Web Worker and Iframe43%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Type-Safe and Promisified API for Web Worker and Iframe

Hacker News2
Re
Rescue – type-safe exceptions for Python38%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Rescue – type-safe exceptions for Python

Hacker News7
Po
Pocketstore – Type-safe session/localStorage with expiry23%Launch Intel prediction score: how likely this product is to succeed on its source platform, based on its name, tagline, and description.

Pocketstore – Type-safe session/localStorage with expiry

Hacker News1