Click to See Complete Forum and Search --> : new and delete with multi_dimension array
slscus
October 9th, 2002, 02:18 PM
I need to dynamically generate a multi_demension array. I know
how to do it with calloc() and free(), Could you tell me how to
do it with new() and delete()?
Thanks
//Code:
// Memory allocation
int *** pSomething
pSomething = (int ***)calloc(n1, sizeof(int **));
for(int i=0; i<n1; i++) {
pSomething[i] = (int **)calloc(n1, sizeof(int **));
for(int j=0; j<n2; j++) {
pSomething[i][j] = (int *)calloc(n2, sizeof(int *));
}
}
//free
for(int i=0; i<n1; i++) {
for(int j=0; j<n2; j++) {
free pSomeThing[i][j];
}
free pSomeThing[i];
}
free pSomeThing;
//End of the code
Yves M
October 9th, 2002, 02:38 PM
Well, hum, I don't like this way of accessing arrays. My c-style arrays are always :
long *pArray;
long xdim, ydim, zdim;
pArray = (long *) calloc(sizeof(long) * xdim * ydim * zdim);
this translates well when using new instead of calloc.
To quote the standard STL response, you could use std::vector and write a wrapper class for a three dimensional array.
slscus
October 9th, 2002, 03:15 PM
Thanks, but if I did this way, I cann't access the data by using
pArray[i][j][k]. ???
PaulWendt
October 9th, 2002, 03:43 PM
Hi,
If you're interested in multi-dimensional vectors, check out this
old codeguru posting:
http://www.codeguru.com/forum/showthread.php?s=&threadid=210478&highlight=vector+operator
Here's a link from the last discussion that is relevant:
http://www.cuj.com/articles/2000/0012/0012c/0012c.htm
--Paul
Yves M
October 9th, 2002, 03:49 PM
Originally posted by slscus
Thanks, but if I did this way, I cann't access the data by using
pArray[i][j][k]. ???
True, you'll have to do pArray[((i * ydim) + j) * zdim + k];
It's a bit uglier, but not a real problem.
But have a look at the links provided by Paul, you'll get a lot of benefits for using that. Not the least is that you'll get an introduction to STL if you don't know it already ;)
room_yuy
October 9th, 2002, 10:13 PM
quote from Yves:
code:--------------------------------------------------------------------------------
long *pArray;
long xdim, ydim, zdim;
pArray = (long *) calloc(sizeof(long) * xdim * ydim * zdim);
--------------------------------------------------------------------------------
i think should be
-------------------------------------------------------------------------------
const long xdim=NUMBERx,ydim=NUMBERy,zdim=NUMBERz;
long *pArray=new long[xdim*ydim*zdim];
......
delete [] pArray
---------------------------------------------------------------------------------
JamesSchumacher
October 11th, 2002, 07:51 PM
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
int main(int _argc,char * _argv[]) throw()
{
srand(::GetTickCount());
int *** pPointerToPointerOfPointers = new int **[10];
for (int i = 0; i < 10; ++i)
{
pPointerToPointerOfPointers[i] = new int *[10];
for (int x = 0; x < 10; ++x)
{
pPointerToPointerOfPointers[i][x] = new int(rand());
}
}
for (int p = 0; p < 10; ++p)
{
for (int q = 0; q < 10; ++q)
{
std::cout << "The value of at index " << q << " of the index " << p << " is " << *(pPointerToPointerOfPointers[p][q]) << std::endl;
}
}
// deletion
for (int r = 0; r < 10; ++r)
{
for (int s = 0; s < 10; ++s)
{
delete pPointerToPointerOfPointers[r][s];
}
delete [] pPointerToPointerOfPointers[r];
}
delete [] pPointerToPointerOfPointers;
return 0;
}
That answers the question - using new and delete to accomplish this. (The question was not about std::vector - although, that is a viable solution to what he wants to do - but he asked for new/delete) It is best to answer how to do something, the way the person asks how to do it - that's what he asked for. In this case, it's a question of C++ logic - knowing the answer and understanding how it works will make a better programmer.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.