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
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];
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];
}
}
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 "delete []" 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 "delete" what you create with "new", 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!
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
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.