Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO DECLARE MULTIDIMENSIONAL ARRAY DYNAMICALLY ? 2

Status
Not open for further replies.

nawaf

Programmer
Jun 11, 2001
12
0
0
KW
Hello,
I was wondering of O can declare multidimensional array dynamically?
Thanks
 
I would use the new and delete operators in C++.

const int Rows = 1000;
const int Cols = 100;

float **matrix;

// Allocate a pointer to an array of floats
matrix = new float *[Rows];

// Allocate each row individually
for (int i = 0; i < Rows; i++)
matrix = new float[Cols];

// Delete each row
for (int i = 0; i < Rows; i++)
delete [] matrix;

// Delete the pointer to the array
delete [] matrix;
 
Ooops, you can't use bracket i bracket on this board because it's interpreted as markup for italics, so my code did not post correctly up above. I changed the subscript to j to make it render correctly:


const int Rows = 1000;
const int Cols = 100;

float **matrix;

// Allocate a pointer to an array of floats
matrix = new float *[Rows];

// Allocate each row individually
for (int j = 0; j < Rows; j++)
matrix[j] = new float[Cols];

// Delete each row
for (int j = 0; j < Rows; j++)
delete [] matrix[j];

// Delete the pointer to the array
delete [] matrix;
 
Don't forget, what a moltidimensional array is too memory expensive. For example a 3D array 10x10x10 of chars will take 1000 bytes. But if are ints, or some complex classes? and the array is 4D, 5D... ND? And how about each dimension will take more than 10 elements? John Fill
1c.bmp


ivfmd@mail.md
 
Right, I would not always recommend a multidimensional array either, especially dynamic. When created inside a function, it is both memory expensive and processing expensive. Memory allocation and de-allocation are quite time consuming.

The example I gave is a classic 2x Row by Columns matrix of data which is useful because it is more easily understandable for a maintenance programmer than other data structures might be.
 
I'm converting my code from linux c++ to visual c++. Previously I calculated my array size is in the program, then declared it. For example, I'd read a couple numbers from a file, multiply them, and declare: double matrix[number]; I'm having trouble doing this in visual c++. Is there a way to do this, or do i have to use the dynamic allocation method?
 
Hmmm, it looks like I have to use this dynamic method.... Is there any easy way to make multiple arrays of the same size. i.e.:
double Ucyc[DataSets+1][DataPoints+1];
double Uturb[DataSets+1][DataPoints+1];
double Vcyc[DataSets+1][DataPoints+1];
double Vturb[DataSets+1][DataPoints+1];
double Uorg[DataSets+1][DataPoints+1];
double Vorg[DataSets+1][DataPoints+1];
 
It seems like the best way to do this is with the form:

double **Ucyc, **Uturb = new double *[DataSets+1];
for(j=0; j<=DataSets; j++)
{
Ucyc[j] = new double [DataPoints+1];
Uturb[j] = new double [DataPoints+1];
}

Am I right?
 
Can somebody help me figure this out. It compiles, but then I get an access error at execution. Thank you

int ***map, ***map_2 = new int **[DataPoints+1];

for(int j=0; j<=DataPoints; j++)
{
map[j] = new int *[Displacements+1];
map_2[j] = new int *[Displacements+1];
for(int k=0; k<=Displacements; k++)
{
map[j][k] = new int[2];
map_2[j][k] = new int[2];
}
}

DataPoints 224
j 0
+ map 0xcccccccc
+ this 0x0012fe58 {CGambit10Dlg hWnd=0x017a04d8}

00436B20 CXX0013: Error: missing operator
 
Ok, i fixed it, I changed it to
int ***map, ***map_2;
map = new int **[DataPoints+1];
map_2 = new int **[DataPoints+1];
and then the loops

However does anybody know a method for making this less tedious? I have many, many arrays I have to declare like this, all of the same size.
 
What about this:

int my2c(int i, int j)
{
double (*B)[j] = NULL;
B = new double[j];
...
delete B;
...
return 0;
}
I am more C persone, so it can be wrong. Just an idea....
 
It is my understanding that the &quot;delete []&quot; is used after the array is no longer needed to deallocate the memory, correct?

Also, I get a Debug Assertion Failed! error with the expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

this is a result of the following block of code
for(j=0; j<=DataPoints; j++)
{
for(int k=0; k<=Displacements; k++)
{
delete [] map[j][k];
delete [] map_2[j][k];
}
delete [] map[j];
delete [] map_2[j];
}
delete [] map;
delete [] map_2;

which was allocated with the following code:
int ***map, ***map_2;
map = new int **[DataPoints+1];
map_2 = new int **[DataPoints+1];

for(int j=0; j<=DataPoints; j++)
{
map[j] = new int *[Displacements+1];
map_2[j] = new int *[Displacements+1];
for(int k=0; k<=Displacements; k++)
{
map[j][k] = new int[2];
map_2[j][k] = new int[2];
}
}

Can somebody please help me fix this, I'm not sure whats wrong? Also, what will the ill effects be if I just comment out these delete statements?
 
No problems here when I execute your code... No assertions failures, nothing.

If you do not &quot;delete&quot; what you create with &quot;new&quot;, what happens is that the memory allocated for your variables will not be freed when the program exits.

For example, I had a very large variable I allocated with new (it took a couple of megabytes of memory), and I did not delete it at first. I could see Windows' Swap file grow every time I executed my program! Until there was so much memory used by my leaks that the system crashed!

Vincent
 
Hmmm, I'll keep checking back here too, but I started a new thread -- thread116-111019 -- to deal more specifically with assertion failures. Thanks for your feedback Vincent
 
I found my problem: map = map_2;

obviously you can't do it like that, and must copy each array element individually.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top