2 ms·
> What happens when you have an array with more elements than can be expressed by an int? You probably couldn't have an array that big and even if you could, i
by wvenable 17y ago
> What happens when you have an array with more elements than can be expressed by an int?
You probably couldn't have an array that big and even if you could, it's likely this function would accept it at compile time. The problem here is that you're passing it an array that it is advertised to handle, yet it fails.
- Groxx 17y agoIt's pretty easy, actually. Make two arrays, and have the end of one act like a linked list to the start of the second. When a [x] value is larger than the size of the first, just subtract the first's size from x, and find that value in the second. It's even still O(1) time. Viola, you've now got nearly double the storage of what you can reference, and it'll work in a generic case, but you'll never be able to refer to anything larger than your index-number can reach. To make matters worse, do you know if your library isn't doing this for you? It'd allow you to extend your array without having to copy the whole thing, which saves a LOT of time on frequently-lengthening arrays. The extremely small cost of checking an int's size before actually accessing the sub-array necessary is often negligible compared to duplicating potentially billions of values.
- jedbrown 17y agoIt's not at all uncommon to have an array that big these days. But the API should use size_t for the array size (or whatever is available in your language of choice).