6 ms·
This wouldn't be a problem if "int" was defined as the same size as size_t. The solution is probably to change all those functions to take a parameter of size_t
by phkahler 5y ago
This wouldn't be a problem if "int" was defined as the same size as size_t. The solution is probably to change all those functions to take a parameter of size_t instead of int.
IMHO one should always be using C99 types instead of int, but Linux predates that.
Also, shouldn't that implicit conversion cause a compiler warning?
- kenniskrag 5y agoso you mean int_fastX_t or int_leastX_t?
- tialaramex 5y agoLinux apparently provides a similarly named set of sized integer types to Rust, ie: s8 u8 s16 u16 s32 u32 s64 u64 But of course getting C programmers to use these integer types rather than the ones they grew up with isn't easy.
- foobiekr 5y agoI did a lot of C - a lot - in the mid-90s through the mid/late 2000s and have never seen any sizable C code base where explicit sizes were not the norm throughout - u_int32_t etc. I think this is not the problem, it's the implicit conversions.
- masklinn 5y agoI don't think that stops C's implicit conversions either, does it?
- dezgeg 5y agoIt doesn't. They are in the end just typedefs to the basic types like int, long, etc.
- cesarb 5y ago> This wouldn't be a problem if "int" was defined as the same size as size_t. That would lead to a hole in the type sequence (char <= short <= int <= long <= long long) for 64-bit targets (where int is the 32-bit type while size_t is 64 bits). > IMHO one should always be using C99 types instead of int, but Linux predates that. On the other hand, on Linux "long" has always been defined as the same size as size_t, so using "long" instead of "int" everywhere could also be an option.
- chippiewill 5y ago> IMHO one should always be using C99 types instead of int, but Linux predates that. "Always" is a strong way of putting it, there are often times where it makes sense to use the platform's "natural" word sizes (which is the entire point of having `int` `long` `long long` etc.)
- phkahler 5y ago>> there are often times where it makes sense to use the platform's "natural" word sizes In those cases we probably don't care about the full range of the larger types, so it doesn't hurt to use the smallest type for a range of expected values. If it does make a difference, the program will behave differently when compiled on a different arch or even a different compiler. But maybe "generally" instead of "always". OTOH even I am guilty of using an int to loop over an array.
- pcwalton 5y ago> This wouldn't be a problem if "int" was defined as the same size as size_t. ILP64 causes a lot of problems, most notably needlessly-increased memory usage and, in C, the inconvenience of requesting a 32-bit type when int is 64-bit. It's rather uncommon to actually need the extra 64-bit range except when describing pointer addresses and memory/disk sizes, both of which benefit from an explicit intptr_t/size_t type for readability if nothing else.
- saagarjha 5y agoILP64 also solves a lot of problems if you don’t define overflow to be UB.