Skip to content

Commit

Permalink
cleaned up memory_alloc.hpp slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
jewettaij committed Feb 15, 2020
1 parent 1942fa3 commit 2ea8399
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions include/matrix_alloc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,38 @@

namespace matrix_alloc {

/// @brief Allocate a 2-dimensional table row-major order.
/// @brief Allocate a 2-dimensional array. (Uses row-major order.)
template<typename Entry>
void Alloc2D(size_t nrows, //!< size of the array (number of rows)
size_t ncols, //!< size of the array (number of columns)
Entry ***paaX //!< pointer to a 2-D C-style array
Entry ***paaX //!< pointer to a 2D C-style array
);

/// @brief This function is the corresponding way to deallocate
/// arrays that were created using Alloc2D().
/// @brief Deallocate arrays that were created using Alloc2D().
template<typename Entry>
void Dealloc2D(Entry ***paaX //!< pointer to 2-D multidimensional array
void Dealloc2D(Entry ***paaX //!< pointer to a 2D C-style array
);


// --- IMPLEMENTATION ---

// ---- IMPLEMENTATION ----


template<typename Entry>
void Alloc2D(size_t nrows, // size of the array (number of rows)
size_t ncols, // size of the array (number of columns)
Entry ***paaX) // pointer to a 2-D C-style array
Entry ***paaX) // pointer to a 2D C-style array
{
assert(paaX);
// Allocate a conventional 2-dimensional
// pointer-to-a-pointer data structure.
*paaX = new Entry* [nrows];
(*paaX)[0] = new Entry [nrows * ncols];
*paaX = new Entry* [nrows]; //conventional 2D C array (pointer-to-pointer)
(*paaX)[0] = new Entry [nrows * ncols]; // 1D C array (contiguous memor)
for(size_t iy=0; iy<nrows; iy++)
(*paaX)[iy] = (*paaX)[0] + iy*ncols;
// The caller can access the contents using (*paaX)[i][j] notation.
// The caller can access the contents using (*paaX)[i][j]
}

template<typename Entry>
void Dealloc2D(Entry ***paaX) // pointer to 2-D multidimensional array
void Dealloc2D(Entry ***paaX) // pointer to a 2D C-style array
{
if (paaX && *paaX) {
delete [] (*paaX)[0];
Expand Down

0 comments on commit 2ea8399

Please sign in to comment.