How much memory does an array with one high index element use?

Would this code run around 4_000_000 bytes of memory?

my uint32 @array;
@array[1_000_000] = 1;

      

+3


source to share


1 answer


If you assign an item 1_000_000

and each item is 4 bytes, that would be 4_000_004 bytes of memory. So, strictly speaking, the answer is "No" :-)



But less pedantic: the built-in arrays are guaranteed to be laid out sequentially in memory, so this assignment allocates at least one block of 4 x 1_000_001 = 4_000_004 bytes of memory. As Christoph said in his comment, if you want to make sure that this is all he will ever highlight, you need to make it a formed array. You will also receive bonuses for the upper bound.

+9


source







All Articles