Can someone please take a look at my code to ensure there aren't any memory leaks or (array) overrides or anything else which could be a bad practice in my code ...
Here's my code:
#include <iostream>
using namespace std;
template <typename T>
void create2DMatrix(int rows, int cols, T **&matrix);
template <typename T>
void destroy2DMatrix(int rows, T **&matrix);
int main()
{
double ** test; /* Needed for our Dynamic 2D Matrix */
create2DMatrix(2, 1, test); /* Create a 2D Matrix */
test[0][0] = 23.59; /* Put some data in the Matrix */
cout << test[0][0] << endl; /* Print the data on the screen */
destroy2DMatrix(2, test); /* Clean up (destroy our 2D Matrix) */
return 0; /* Exit the program ... */
}
template <typename T>
void create2DMatrix(int rows, int cols, T **&matrix)
{
/* Create a 2D Dynamic Array / Matrix */
matrix = new T * [rows];
for(int i = 0; i < rows; i++)
{
matrix[i] = new T[cols];
}
}
template <typename T>
void destroy2DMatrix(int rows, T **&matrix)
{
/* Destroy a 2D Dynamic Array / Matrix */
for(int i = 0; i < rows; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
Thanks in advance !