from Gulliver;
hi there.
i have engineered/fiddled the following code that gives no errors;
-----------------------------------------------------------------------------------
double **matrix; //declares matrix (double pointer)
//allocate the array somewhere else in code
int size = ~decided at runtime;
matrix = new double * [size]; //allocates an array of pointers to matrix that point to type double
for (int i=0;i<size;i++){
matrix[i] = new double [size];} //allocates an array of size to each pointer in first array
//use array(s)
matrix[2][3] = 12.984;
//destroy the matrix
for (i=0;i<size;i++){
delete [] matrix[i];} //deletes each array within matrix
delete [] matrix; //deletes the array of pointers
-----------------------------------------------------------------------------------
i did this because new() doesn't let me dynamically allocate multidimensional arrays.
The code seems to work, but i'm worried that i have abused new() and delete().
Have i freed all of the memory i allocated?
When using matrix[2][3], am i writing to memory i have allocated?
i receive no errors at runtime or from the compiler....
but i'm very new to c++, so if anyone can comment the above code i would be very grateful.
If it is good code, then it should go in someone's book, as others could find it useful....
....but if it isn't i'd really like to know!
thanks