Quote:Original post by PrestoChung
Speed of access to the hMap array is critical, as it will be accessed many times every frame so I did not want to use std::vector.
That's fine; the vector won't make it any slower to access the memory if you're not resizing it. It will just sit there with, under the hood, a pointer to a single chunk of memory, just the same as the one you would allocate and manage yourself. It's just a more organized way of doing it: the supporting code to deallocate the memory, set up the initial objects (by taking a single "prototype" object and copying it for each element) etc. is already written for you.
However, for a rectangular, 2-dimensional array, you should instead consider boost::multi_array. Making a vector of vectors
will cost something in terms of efficiency - and
so does your current approach - because each "row" is allocated separately. That means you waste memory for the pointers to rows and can lose performance because of the rows being spread out across the memory space instead of being in a single chunk. The boost::multi_array container is designed to allocate a single chunk but make it behave like a statically-allocated, multi-dimensional array. (It does this by also remembering the dynamic size of each dimension of the array, and using those numbers to do math to index into the storage - plus a bunch of other neat magic that is
very difficult to do yourself. How difficult, you say? Well, I tried a few years ago to reproduce the basics of it, and while I had a pretty solid idea of how, I got bored and gave up. ;) )