Please don't debug my code, that was a quick snippet to illustrate the idea of the multiple pointer method. In a real program I would initialize pointers to NULL, check for new returning NULL, and yada-yada. Of course, you can set new to throw bad_alloc on allocation failure if you prefer that behavior, and, of course, in that case, you have to catch it and do cleanup and stuff. Personally though, for reasons that are beyond the scope of this topic, I absolutely hate the exception programming system so I prefer doing it old-school style and check for return values instead.
Also, multiple push_back *is* slow when you have large amount of bytes to store. My RPG loads its assets and data from a custom archive format I designed, and, because the buffers need to have variable sizes, I decided to use a std::vector and call push_back to store the decompressed data bytes into the vector buffer. It was quite slow. Because I called reserve, I expected it to be super fast, and to be honest I have no idea why it was slow, but for whatever reason, it didn't work well and there was considerable slowdowns. I guess I could have used resize and write directly in the buffer using the [] operator (it would probably have fixed the problem) but I went straight to using new and delete because I knew this would work.
The overhead caused by vectors is that when you resize a vector (declaring a vector by passing it an initial size is the same thing), the STL does not simply allocate sufficient memory: in the case of a growing vector, it passes through all new items to initialize them. This is unlike the new operator, which just allocates the data while leaving the garbage inside. (Unless you are in debug mode.) In a nut shell: vector = allocation + initialization, New = allocation only. So, in the example posted above, the overhead is not at the allocation where you displayed the assembler, it's at the declaration.
Although it will not affect the TC if it's done correctly (more on that farther), another thing I recently discovered concerning vectors is that they can potentially waste memory. As you probably know, vectors are optimized to be resizable arrays whose length changes dynamically, and in order to prevent too much reallocation, the object is programmed to increase its size by 50% everytime it needs to grow. And that's great because it's a nice compromise between the speed of an array and the power of a linked list when you need an array of unknown size. However, as far as I know, the buffer never shrinks. What this means is that, if you have some huge chunk of data that you store to load data, let's say a 1MB buffer, then the vector will always have 1MB allocated even if your next loads are 300 bytes. Only the destructor seems to free that memory.
Don't believe me? See for yourself. The second cout is to illustrate the initialization process.
#include <vector>#include <iostream>using namespace std;int main(){ int i; vector< int > v; v.resize( 1000000 ); v.resize( 50 ); cout << "Capacity: " << v.capacity() << endl; cout << "Index 23: " << v[ 23 ] << endl; cin >> i; return 0;}
STL vectors seem to be internally wired to be resizable. In the case of the TC question, if I understand correctly, the multi-dimensional array is sized once, when the map is loaded, then it remains constant until another map is loaded. So that is not something I'd use a vector for: I just need an array, and the only time it is resized is while loading while the screen is black. Still, if the TC wants to use the vector method, it is imperative when repopulating the vectors to call clear on the vector of vector instead of resize or it would lead in waste of memory after unloading a really large map.
Of course, I know that memory is cheap for PCs and that the performance losses are unnoticable 99% of the time. But I believe it's a bad habit to use vectors everywhere. Not saying I hate vectors, I use them a lot and I suggest people to use them as well. It's just that I suggest reserving those for arrays that are constantly changing in size.
In any case, this is getting off-topic. I just wanted to present a template without boost or STL.