I’m not too concerned about time efficiency (the operation will be rare), but rather about memory efficiency: Can I grow the array without temporarily having all the values twice?
Is there a more efficient way to grow a large array than creating a new one and copying over all the values? Like, concatenating it with a new one?
What about having fixed-size arrays stored in another array and reallocate / copy that top-level one? Would that leave the actual values in place?
I’m aware of ArrayList, but I need a lot of control about accessing the array and the access needs to be very fast. For instance, I think I prefer a[i] to al.get(i).
The main reason why I care about this is that the array in question (or a number of such arrays) might very well occupy a large enough portion of main memory that the usual strategy of creating a double sized copy before discarding the original might not work out. This may mean that I need to reconsider the overall strategy (or up my hardware recommendations).
No. And probably there is no language, that guarantees growing an array will always take place without copying. Once you allocate the space for the array and do something else, you most likely have other objects in memory right after the end of the array. At that point, it’s fundamentally impossible to grow the array without copying it.
You mean have an array of arrays and treat it as one large array consisting of a concatenation of the underlying arrays? Yes, that would work (the “faking it by doing indirection” approach), as in Java,
Object[][]is simply an array of pointers toObject[]instances.