Lesson 13 | Dynamic multidimensional arrays |
Objective | The allocate() function |
allocate()
function in our dynamic, multidimensional array program is used for runtime array allocation.
void allocate(int r, int s, twod& m){ m.base = new double*[s]; assert (m.base); for (int i = 0; i < s; ++i){ m.base[i] = new double[r]; assert (m.base[i]); } m.row_size = r; m.column_size = s; }
allocate()
function uses new
to allocate:doubles
double
is allocated. Each of these pointers have the base address for a row of doubles
.
This space is allocated off the heap iteratively using a for
loop.
Notice the use of assert
to test if the memory allocation was successful.