2 ms·
My array types are not meant to be changed, so no capacity is needed. I have separate resizable ones.
by gavinhoward 3y ago
My array types are not meant to be changed, so no capacity is needed. I have separate resizable ones.
- tialaramex 3y agoWhen you say "array types" it seems like people expected you meant a growable array (C++ std::vector, Rust's Vec, or say Java's ArrayList) But if you meant an actual array as in Rust's [T; N] then it's weird to talk about them as if they've got some specific size, their size is a parameter (N) of the type. The size of Rust's [u8; 16] or C's unsigned char [16] is 16 bytes but like, duh. And there's no magic here, [u8; 24] or unsigned char [24] is 24 bytes.
- gavinhoward 3y agoIn 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...