Author Topic: array index  (Read 1770 times)

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 266
  • UNUS SED LEO
    • View Profile
array index
« on: 2012-Feb-12 »
hello all.
where is the mistake?
with  array3dLen_C = 80 all OK. With upper indices there is the error:
EXCEPTION_STACK_OVERFLOW.

ciao

INLINE
      int tc, zc, yc;
      int array3dLen_C=80;
      int array3d_C[array3dLen_C][array3dLen_C][array3dLen_C];
      for (tc = 0; tc < array3dLen_C; tc++)
         for (zc = 0; zc < array3dLen_C; zc++)
            for (yc = 0; yc < array3dLen_C; yc++)
                array3d_C[yc][zc][tc] = i;
ENDINLINE

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 496
    • View Profile
Re: array index
« Reply #1 on: 2012-Feb-13 »
This is a C question, not GLBasic.

The problem is declaring a big array in the stack. Your array has 80x80x80x4 = 2048000 bytes = 2mb. But this array is created in the stack if used inside a function, a little memory space used for parameters and calls. The solution is use the data segment (using the static modifier to make global), or use dynamic memory (using the new operator).

You can use a struct array with x,y,z members too. This is faster and easier to manage than a 3 dimensions matrix.

// using static for put it in the data segment
static int array3d_C[array3dLen_C][array3dLen_C][array3dLen_C];

// using pointers for dinamic arrays
// using one dimension for the elements is easier here
int *array3d_C = new [array3dLen_C*array3dLen_C*array3dLen_C];
« Last Edit: 2012-Feb-13 by hardyx »

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 266
  • UNUS SED LEO
    • View Profile
Re: array index
« Reply #2 on: 2012-Feb-15 »
Thank you hardyx
now work!!!  =D
Ciao

      INLINE
         int tc, zc, yc;
         const int array3dLen_C=81;
         static int array3d_C[array3dLen_C][array3dLen_C][array3dLen_C];
         for (tc = 0; tc < array3dLen_C; tc++)
            for (zc = 0; zc < array3dLen_C; zc++)
               for (yc = 0; yc < array3dLen_C; yc++)
                   array3d_C[yc][zc][tc] = i;
      ENDINLINE

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10746
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: array index
« Reply #3 on: 2012-Feb-16 »
static makes it not really better. You should allocate it on the heap using new/delete.

Offline Qedo

  • Dr. Type
  • ****
  • Posts: 266
  • UNUS SED LEO
    • View Profile
Re: array index
« Reply #4 on: 2012-Feb-25 »
Thank you for your helps.
Here the 2 methods, static and dymanic array, and benchmark


      INLINE
         int height4d_C = array4dLen_C; //27
         int depth4d_C = array4dLen_C * array4dLen_C; //729
         int time4d_C = array4dLen_C * array4dLen_C * array4dLen_C; //19683
         const int array4dLen_C=27;
         const int array1dLen_C=array4dLen_C * array4dLen_C * array4dLen_C * array4dLen_C;
         
         // Fill STATIC STACK 1d array simulating 4d         
         static int array1d_C[array1dLen_C];
         for (tc = 0; tc < array4dLen_C; tc++)
            for (zc = 0; zc < array4dLen_C; zc++)
               for (yc = 0; yc < array4dLen_C; yc++)
                  for (xc = 0; xc < array4dLen_C; xc++)
                     array1d_C[xc + yc * height4d_C + zc * depth4d_C + tc * time4d_C] = i;

         // Fill DYNAMIC HEAP 1d array simulating 4d
         int *array4dd_C;
         array4dd_C = new int[array4dLen_C * array4dLen_C * array4dLen_C * array4dLen_C];
         for (tc = 0; tc < array4dLen_C; tc++)
            for (zc = 0; zc < array4dLen_C; zc++)
               for (yc = 0; yc < array4dLen_C; yc++)
                  for (xc = 0; xc < array4dLen_C; xc++)
                     array4dd_C[xc + yc * height4d_C + zc * depth4d_C + tc * time4d_C] = i;
         delete[] array4dd_C;
      ENDINLINE
      
Time comparation for 531441 int array
STATIC STACK ARRAY= 0,6 seconds
DYNAMIC HEAP ARRAY= 1,8 seconds

Ciao

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 496
    • View Profile
Re: array index
« Reply #5 on: 2012-Feb-25 »
static is faster, but the array memory is used in all the application life. If you use the heap, you can free the space when you don't use.