3 ms·
Not really. Arrays are limited to an int in size. You would be using more memory for the calculation and have to cast the value down to a 32 bit value to use
by MattPalmer1086 2y ago
Not really. Arrays are limited to an int in size. You would be using more memory for the calculation and have to cast the value down to a 32 bit value to use as an array index.
Or you could just write the code so it isn't vulnerable to integer overflow.
- rob_c 2y agoIdeally the index should support int64 with int being a synonym on a 64bit platform by default. If not yes frankly you're into such unexpected behaviour territory that you should check your whole codebase rather than rely on stuff working just because it compiled. And we all know how everyone loves writing and understanding integration tests... (I personally do but most problems in the industry wouldn't be a thing if more people stopped to write them)
- bobmcnamara 2y agosize_t, you mean?
- rob_c 2y agoWell yes by inference
- hedora 2y agoJava doesn’t have a type that corresponds to size_t. It only has signed integer types, so the closest match is ssizet_t (but even then you need to figure out how many bits that is on your architecture).
- bobmcnamara 2y agoJ8 has unsigned integer arithmetic at least.
- touisteur 2y agoWhich is sad (array size limited to an int) and has always annoyed me, coming back from Ada where the index of an array is just another discrete type (including boolean, enum type, and ranged integers).