Quote Originally Posted by CVMichael View Post
Correct! it MIGHT overflow! ... let's see WHEN?

So, a Long data type is 32 bits, but we are using a signed integer, so 31 bits, and that is 2,147,483,648... those are array items...
Now lets see how much memory you would need to store that many items (so that you get the overflow)

So, internally, each item is a pointer to another location in the memory (basically an array of pointers) each one pointing to a string structure (a BSTR, I can't find anywhere the actual structure of BSTR), similar to this one, and that structure is 12 bytes (on a x86 processor) (and there is another pointer to the actual string... and lets say on average the string is 10 bytes? so let's do the math...

2^31 * 4 * 12 * 10 = 1.0307922e+12
that number is in bytes... so let's convert that into GBytes:
(2^31 * 4 * 12 * 10) / 1073741824 = 960 GBytes

So... you will get an overflow error if the total memory your array takes is approximately (with a few assumptions) 960 GBytes that has to be stored in RAM (by the way)... and again... this is if your average string is 10 bytes long... and since all strings are unicode, that means 5 characters each string

So... in conclusion... let's add another operation to the loop to slow it down, because we might get an overflow sometime in the future when computers will have a few terabytes of RAM... it might not be that much far away...

But.... good catch though


[Edit]... if you port this code to VB.net then the Long data type is 64 bits.... so.... do you still think you might get an overflow?
Yes, it's always possible.

This calculation refers to the array index, not the array contents; you only need greater than 2^30 items in the array, regardless of element size which is just over a billion items, which is not insofar as your gigantic amount that unreasonable to expect.

Nevertheless, even if this limit is way beyond the expected maximum ever likley, having that bug in to save one operation (which is likely optimised out by the compiler) is premature optimisation and is therefore evil.

Still saving 0.0000000000000000000000000001 extra seconds available might be worthwhile, I suppose - especially if you're sorting on greater than a billion records ....