I found that changing my approach was very helpful when faced with a similar problem.
The vector of vectors filled the same task, avoided the obstacles of memory allocation, and kept the same familiar shorthand. There may be other pitfalls, but I have not met them yet.
//Declaration of mValues, undefined size: std::vector< std::vector<char> > mValues; //Filling of mValues: int max_x = 100 ; int max_y = 100 ; char char_foo = 'a'; for ( int x = 0; x <= max_x; ++x ) { vector<char> temp; for ( int y = 0; y <= max_y; ++y ) { temp.push_back( char_foo ); } mValues.push_back( temp ); } // Referencing with familiar index notation: mValues[a][b]; //The a-th row b-th element
If you're struggling with arrays but really want a familiar indexing language, I found this to be a good alternative.
Note that indexing order A then B will be critical for memory usage when calling this data. Non-compliance with information in order A, B will be very problematic if performance is a problem.
source share