Allocate dynamically using new and delete? |
I wouldn't recommend doing that - it has problems of it's own.
Ideally, you can just use an appropriate STL container, and avoid all these hassles.
The containers implicitly allocate their memory on the heap - you don't have to worry about it. They also have safeguards in that one cannot overrun the end of the container, they have things such as
size()
functions, one can use range based for loops instead of iterators over the whole container.
One other thing, avoid magic numbers like
15
in your code, make them a
const
variable, and use that variable name in the code.
Unfortunately, one cannot have a container of references, but there is
std::reference_wrapper
http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
So with your 2D array, it seems it has a
const
15 columns, but the number of rows varies. So you could have a
std::vector
of
std::array
(size 15) , of type of the
std::reference_wrapper
above.
std::array
can have a variable as the size of it to start with, but one can't change that afterwards.
Good Luck !!