Original Post
I have a 2D array that is dynamically allocated and used to display a tile map in a map editor program... it's really just linear memory of size [rows * columns] that can be accessed like map[r][c] the way the pointers are set. It works well and gets the job done, but I have some concerns when it comes to resizing the map. By default, the map is 32 by 32 tiles, and the user is allowed to add rows and columns as well as delete them and resize to any dimension. At maximum, I expect it to be about 256 by 256 tiles. The problem I see is that the simple way of resizing by creating the new map array and then copying in the old one can take up a bit of memory. With the current worst case scenario, it'll take about 8MB for a resize operation, which isn't horrible, but I think I could do better. Using the memory pool I have, I don't see any possible way of appending or deleting a row or column, so I was hoping there were some ideas out there. One idea was to have the 2D array set up so that the memory is not linear, but so that each row number of the array points to an allocated area of memory that makes up each row. When resizing, allocate a new list of row pointers and then copy the old pointer values in with the correct locations, then just allocate or free the row being deleted. For columns, I would have to step through every row and copy the information in, then delete the old one for every row there is. It sounds like it's a lot of work, but only a single row of tiles is being allocated at a time, so it's just an additional 1.6KB for a resize in the worst case. Should this be something I could work on, or am I over reacting about memory conservaition? Suppose I up the map max size to 512 * 512... the total resize operation would take 32MB worst case. If there are any ideas or articles about similar operations, I'd be glad to listen.