-
Allocating memory...
Hi all!
I have a problem. I need to be able to modify the size of a multidimensional array, and I made a li'l test doing it with an array that only has one dimension successfully... (i'm using "php" tags rather than code tags cuz well.... it just looks cooler!)
PHP Code:
int max = 8; // initial size of aray
int* a = new int[max]; // allocated on heap
for (int j=0; j<max; j++)
{
a[j]=j;
}
max = max / 2; // halve the previous size
int* temp = new int[max]; // create new bigger array.
for (int i=0; i<max; i++)
{
temp[i] = a[i]; // copy values to new array.
}
delete [] a; // free old array memory.
a = temp; // now a points to new array.
for (j=0; j<max; j++)
{
printf("%d\n", a[j]);
}
printf("Done - hit a key you ass!");
getch();
The problem is, i've heard some scary **** - that all dimensions other than the first must be static values even when allocating memory in this way. Can anyone help me out with a good method of dynamically creating a 2 dimensional array?
Thanks in advance ppl!
-
~
I forget who originally wrote this code...
Code:
#include <iostream>
using namespace std;
int main (void) {
// 2-dimensional dynamic matrix
int **dynamicMatrix = NULL;
int numRows = 0;
int numColumns = 0;
int row = 0;
int column = 0;
cout << "Enter number of rows: ";
cin >> numRows;
cout << "Enter number of columns: ";
cin >> numColumns;
dynamicMatrix = new int*[ numRows ];
for( row = 0; row < numRows; row++ ) {
dynamicMatrix[ row ] = new int[ numColumns ];
}
// set it all to 5
for( row = 0; row < numRows; row++ ) {
for( column = 0; column < numColumns; column++ ) {
dynamicMatrix[ row ][ column ] = 5;
}
}
return 0;
}
-
oooooooooooooh! :D :eek: :cool: :D :) ;) :p :eek: :D
Thankyou soooo much!