Here is a little 2D dynamic array template that I use a fair bit, I think i found it originally on www.cplusplus.com
// Dynamic 2DArray Template : Dynamically allocated array of pointers to pointers(2D array)
// implemented as a template so that it can hold whatever you want to put in it :)
#include "DarkGDK.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
template < typename T >
T **Allocate2DArray( int nRows, int nCols)
{
//(step 1) allocate memory for array of elements of column
T **ppi = new T*[nRows];
//(step 2) allocate memory for array of elements of each row
T *curPtr = new T [nRows * nCols];
// Now point the pointers in the right place
for( int i = 0; i < nRows; ++i)
{
*(ppi + i) = curPtr;
curPtr += nCols;
}
return ppi;
}
template < typename T >
void Free2DArray(T** Array)
{
delete [] *Array;
delete [] Array;
}
// the main entry point for the application is this function
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbRandomize(dbTimer());
// allocate an array2D with 10 x 10 slots allocated in memory.. **d[10][10]
// the array can be accessed using normal 2D array notation OR pointer notation
// which is faster of course.....
int **p_I = Allocate2DArray< int >(10, 10);
// put some random values into our array..
for(int i = 0; i <=9; i++)
{
for(int iv = 0; iv <=9; iv++)
{
p_I[i][iv] = dbRnd(25);
}
}
// print out the values in the array to prove they are there :)
for(int i = 0; i <=9; i++)
{
for(int iv = 0; iv <=9; iv++)
{
dbText(i*20, iv*20, dbStr(p_I[i][iv]));
}
}
dbText(0, 360, "Press any key to continue.");
dbSync(); dbSync();// put what we've drawn onto the screen now
dbWaitKey();
dbCLS();
// fill array with zeros
for(int i = 0; i <=9; i++)
{
for(int iv = 0; iv <=9; iv++)
{
p_I[i][iv] = 0;
}
}
// print array out again to show its changed..
for(int i = 0; i <=9; i++)
{
for(int iv = 0; iv <=9; iv++)
{
dbText(i*20, iv*20, dbStr(p_I[i][iv]));
}
}
dbText(0, 360, "Press any key to continue.");
dbSync(); dbSync();// put what we've drawn onto the screen now
dbWaitKey();
dbCLS();
// no free our array
Free2DArray(p_I);
while ( LoopGDK ( ) )
{
return; // break the loop right away and quit
// update the screen
dbSync ( );
}
// return back to windows
return;
}
The above array, although a set of pointers to pointers can be used with normal array notation... Also, at it's heart, C++ uses almost this exact same method(pointers to pointers) for its standard multi dimensional arrays. Although the compilers implementation would be much more secure lol, the above is just a simple time saver, as I like to allocate my arrays using variables at runtime to save memory(they are made the size they are needed rather than an arbitrary large value)
The reason that it is a list of pointers to pointers is that you cannot declare multi dimensional arrays in C++ dynamically, with ALL dimensions being dynamic, the first dimension must still be a constant in C++ unless pointers are used.....
EDIT : hmm, some of those headers arent needed, I pasted this snippet from a console app and stuk the GDK stuff in
If it ain't broke.... DONT FIX IT !!!