Type-safe generic data structures in C
iafisher.com
Type-safe generic data structures in C
1–10 of 57 posts
Re: Type-safe generic data structures in C
#2Here's an example of a dynamic array in C in about 20 lines: http://nothings.org/stb/stretchy_buffer.txt
And here's a friendly explanation of how this technique works: https://ourmachinery.com/post/minimalist-container-library-i...
Re: Type-safe generic data structures in C
#3Apart from being a pain in the ass to write (don't forget your escape slashes (\) for line endings!), using these macros makes debugging much harder - you no longer have a stack trace (although GDB is fairly smart about this, it's still less than ideal).
Re: Type-safe generic data structures in C
#4A much more simple alternative way of doing this is to use stretchy buffers. Here's an example of a dynamic array in C in about 20 lines: http://nothings.org/stb/stretchy_buffer.txt And here's a friendly explanation of how this technique works: https://ourmachinery.com/post/minimalist-container-library-i...
Re: Type-safe generic data structures in C
#5Re: Type-safe generic data structures in C
#6Header only implementations of data structures are definitely the only way to accomplish type-safe generics in C, which is a great reason to avoid C! It's part of the reason why all C code bases eventually become a kind of unique, macro filled language of their own once they grow complex enough. It's not enough to know C to start working on this code, you have to know all the ins and outs of the weird macro expansion…
#define SQ(x) x*x
cout cout Macros are pretty much entirely unnecessary and should be avoided. Compilers inline nowadays, so you don’t really gain anything from using a macro when you could have just made a function that gives you type safety and intuitive behavior. If you are trying to define constants, use const, enum, or enum classes. If you are trying to define multiple versions of the same function, use polymorphism or templates. If you are trying to wrap around arbitrary code, use lambdas.
Re: Type-safe generic data structures in C
#7Re: Type-safe generic data structures in C
#8Why would anyone use this when there is C++, Rust, etc? Genuinely curious.
Re: Type-safe generic data structures in C
#9Re: Type-safe generic data structures in C
#10A much more simple alternative way of doing this is to use stretchy buffers. Here's an example of a dynamic array in C in about 20 lines: http://nothings.org/stb/stretchy_buffer.txt And here's a friendly explanation of how this technique works: https://ourmachinery.com/post/minimalist-container-library-i...