Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically allocate or waste memory?

I have a 2d integer array used for a tile map.

The size of the map is unknown and read in from a file at runtime. currently the biggest file is 2500 items(50x50 grid).

I have a working method of dynamic memory allocation from an earlier question but people keep saying that it a bad idea so I have been thinking whether or not to just use a big array and not fill it all up when using a smaller map.

Do people know of any pros or cons to either solution ? any advice or personal opinions welcome.

c++ btw

edit: all the maps are made by me so I can pick a max size.

like image 935
Skeith Avatar asked Dec 06 '22 21:12

Skeith


1 Answers

Probably the easiest way is for example a std::vector<std::vector<int> > to allow it to be dynamically sized AND let the library do all the allocations for you. This will prevent accidentally leaking memory.

like image 76
Mark B Avatar answered Dec 19 '22 16:12

Mark B