4 ms·
In my code, my array types are a pointer and a length, regardless of the length of the array. This is so I can use regular C pointers to pass them to system fu
by gavinhoward 3y ago
In my code, my array types are a pointer and a length, regardless of the length of the array.
This is so I can use regular C pointers to pass them to system functions that expect pointers. Because C still uses just pointers. But I have the length for bounds checking in my own code.
But my resizable types are much bigger. Probably 32 bytes because they store a destructor too. I pass them by pointer because plain pointers are almost always just one item, meaning no bounds checking is necessary.
Yes, that does mean the actual array is two indirections away, but that style gives me a lot of safety because araay indexing is a code smell.
- tialaramex 3y agoThis seems like a lot of lost optimization opportunities compared to a language which gets this right out of the box. Also in most cases people's destructors don't have associated local state, so, they needn't take up space in each object. All C objects have non-zero size, so if you have an object representing the destructor even if it has no state that takes up space. In C++ there's a hack to avoid paying this price, but in C there is not.
- gavinhoward 3y ago> This seems like a lot of lost optimization opportunities compared to a language which gets this right out of the box. Well, yeah, but I hate all other languages [1], and I'm willing to pay the price in C. That same sort of thing allowed me to implement RAII in C, though. [1]: https://gavinhoward.com/2023/02/why-i-use-c-when-i-believe-in-memory-safety/ https://gavinhoward.com/2023/02/why-i-use-c-when-i-believe-i...