I have a 3D game where objects are stored in a 3D array. Every object is a struct of a relatively large size (a few megabytes, 4,194,304 bytes to be precise).
If I store them as an array of raw values: gameObject objects[64][64][8], they are all stored in one place, but every object is allocated, regardless of whether or not it is used.
However, if I allocate the array as an array of pointers: gameObject *objects[64][64][8], unused objects (which may be over half of the entire array) will not be allocated until they are needed, reducing the memory impact, but might be slower because the objects are all over the memory
Considering the tradeoffs between performance and memory usage, which is the best approach? Are my concerns valid?
Since every object is 4MB in size, locality of reference across different objects is almost certainly not an issue. Go with the second approach.
If the numbers you’re giving us are correct, a fully populated array of 128x128x128 4MB objects would require about 8TB of RAM, which makes the first approach somewhat infeasible on typical hardware. 😉