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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Allocating multi-dim array's

Status
Not open for further replies.
Jan 20, 2005
180
US

I current have
int polyline[1024][4];
double vertex[1024][10][2];

I need to change this to
int polyline[2048][4];
double vertex[2048][10][4];

By changing either anything in the vertex I overflow the stack? Wow huh..

I need to allocate the memory for this on the heap, but keep it accessible the way I currently have, Elsewise i will have to go about redoing everything right now into a struct based list which I do not have the timeframe for atm.
 
You are overflowing the stack because there isn't that much space on the stack. You could increase the stack size or as you said, put it on the heap. A similar solution for putting things on the heap has been suggested in one of the earlier threads. Alternatively as cpjust suggested, use vectors. I've never used 3D vectors so I don't know how well they perform.

vectors are great as long as you don't have to look at them in the debugger.

Here is one way of putting it on the heap
Code:
#define POINTS 2048
#define DIMS   4
int** polyline;
double*** vertex, **vpp, *vp;
int i, j;
polyline = new int*[POINTS];
polyline[0] = new int[POINTS * DIMS];

vertex = new double**[POINTS];
vpp = vertex[0] = new double*[POINTS*10];
vp = vertex[0][0] = new double[POINTS*10*DIMS];
for (i = 0; i < POINTS; i++)
{
   if (i > 0)
   {
      vertex[i] = &vpp[10];
      vpp = vertex[i];
      polyline[i] = &polyline[i - 1][DIMS];
   }
   for (j = 0; j < 10; j++)
   {
      if (i == 0 && j == 0) continue;
      vertex[i][j] = &vp[DIMS];
      vp = vertex[i][j];
   }
}
To deallocate
Code:
delete [] vertex[0][0];
delete [] vertex[0];
delete [] vertex;
delete [] polyline[0];
delete [] polyline;
 
I have not used Vectors either...
I have found that I can get my base array to 1280, actually the exact number I think was ?1304?.. I guess until I have time to go through and redo everything in a couple weeks this will have to do..

Alternatively the plan I have is to make a struct to hold the info I need, and for each Polyline to allocate a new struct, as well as the vertex, where the polyline struct holds a ptr to the first vertex.

Going back into C let alone C++ and VC++ after 4 years is a bit hard. Especially after doing 95% of everything in Unix. Contrary, I can still put a TCP/IP Unix application together quickly haha...

Thanks both of you for your ideas... If I get time soon I will look into vectors.
 
> int polyline[2048][4];
If the minor dimensions are constant, you can achieve the whole allocation in one call.
Code:
int (*polyline)[4] = new int[howmany][4];

--
 
Only the 1st extent may vary in C and C++. The compiler must know 2nd, 3rd etc extents (in compile time, before run). So you can't dynamically change minor (native) array extents on the fly in principle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top