[NXC] ArrayInit for 2D arrays

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

[NXC] ArrayInit for 2D arrays

Post by nxtboyiii »

Hi,
is there a way to reset the size of each dimension in an array?

Like this:

Code: Select all

int map[10][20];
//Now I want to set the 1st dimension to 30, and the second dimension to 45
Thanks, and have a nice day,
nxtboy III

programnxt.com
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] ArrayInit for 2D arrays

Post by HaWe »

no, you can't resize a static variable during runtime, that's only possible with dynamic memory allocation like
malloc()
but you will need to have pointers for that...
(I wish NXC had it)
nxtboyiii
Posts: 366
Joined: 02 Oct 2010, 07:08
Location: Everywhere

Re: [NXC] ArrayInit for 2D arrays

Post by nxtboyiii »

But it is possible to resize a 1D variable!!!
Thanks, and have a nice day,
nxtboy III

programnxt.com
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] ArrayInit for 2D arrays

Post by HaWe »

:shock: ?
show me an example, please...
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: [NXC] ArrayInit for 2D arrays

Post by afanofosc »

You can resize any array in NXC using ArrayInit.

Code: Select all

task main()
{
  int foo[10]; // foo has 10 ints in it.
  ArrayInit(foo, 0, 20); // foo now has 20 ints (all zero)
  int bar[10][20];  // bar is a 10 element array where each element is an array containing 20 ints.
  int tmp[]; // you need temporary arrays with N-1 dimensions (down to 1) to initialize an N-dimensional array
  ArrayInit(tmp, 0, 30); // first initialize the temporary array
  ArrayInit(bar, tmp, 15); // bar is now a 15 element array where each element is an array containing 30 ints (all zero).
}
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] ArrayInit for 2D arrays

Post by HaWe »

amazing... :o

will the memory from upsizing be set free when you're downsizing it after using the bigger size for a while?
Or will the bigger size now be allocated in future?
so can you switch memory back and forth?

Code: Select all

  int foo[10]; // foo has 10 ints in it.
  int bar[10]; // bar the same
  
  ArrayInit(foo, 0, 32000); // foo now has 32000 ints (all zero)
  
  // not enough memory also for upsizing also bar[32000] at the same moment
  // so now first downsize foo:

  ArrayInit(foo, 0, 10); 

  // what happens with the rest of 31990 int mem space?
  // can it now be used for bar?
  ArrayInit(bar, 0, 32000); 

  ArrayInit(bar , 0, 10); // and resize back?
  ArrayInit(foo, 0, 32000); //  
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: [NXC] ArrayInit for 2D arrays

Post by muntoo »

doc-helmut wrote:will the memory from upsizing be set free when you're downsizing it after using the bigger size for a while?
Or will the bigger size now be allocated in future?
so can you switch memory back and forth?
I did some tests a long, long time ago in a galaxy far, far away, and I believe the results showed that the memory is put back into the pool. (Otherwise, my program would have crashed due to multiple reallocations... I think.)

Whenever you realloc()ate using ArrayInit(), the memory that the original array "points" to gets free()d, and new memory is malloc()ated.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] ArrayInit for 2D arrays

Post by HaWe »

I wrote a macro for ArrayInit of 2D arrays and made some memory tests,
and the following is fine up to a 122x122 array of int (seems to be close to the variable space limit):

Code: Select all

#define ArrayInit2D(bar, tmp, x, y) { \
  ArrayInit(tmp, 0, y); \
  ArrayInit(bar, tmp, x);  \
}

task main(){
  int ibar[][], itmp[];
  float fbar[][], ftmp[];
  
  int i, j, imax, jmax;
  jmax=imax=122;
  
  ArrayInit2D(ibar, itmp, imax, jmax);
  for (i=0; i< imax; ++i)  {
    for (j=0; j< jmax; ++j)   {
      ibar[i][j]=(i+1)*(j+1);
      TextOut(0,48, "                ");
      NumOut(0,48,ibar[i][j]);
    }
  }


  while (true) { }

}
Last edited by HaWe on 04 Jun 2011, 09:56, edited 2 times in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] ArrayInit for 2D arrays

Post by HaWe »

this is fine for 2 arrays, each 60x60:

Code: Select all

#define ArrayInit2D(bar, tmp, x, y) { \
  ArrayInit(tmp, 0, y); \
  ArrayInit(bar, tmp, x);  \
}

task main(){
  int ibar[][], itmp[];
  float fbar[][], ftmp[];
  
  int i, j, imax, jmax;
  jmax=imax=60;
  
  ArrayInit2D(ibar, itmp, imax, jmax);
  for (i=0; i< imax; ++i)  {
    for (j=0; j< jmax; ++j)   {
      ibar[i][j]=(i+1)*(j+1);
      TextOut(0,48, "                ");
      NumOut(0,48,ibar[i][j]);
    }
  }




  ArrayInit2D(fbar, ftmp, imax, jmax);
  for (i=0; i< imax; ++i)  {
    for (j=0; j< jmax; ++j)   {
      fbar[i][j]=(i+1)*(j+1);
      TextOut(0,32, "                ");
      NumOut(0,32,fbar[i][j]);
    }
  }

  while (true) {}

}
Last edited by HaWe on 04 Jun 2011, 09:56, edited 1 time in total.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [NXC] ArrayInit for 2D arrays

Post by HaWe »

this is fine for 1 array 122x122, the other 10x10:

Code: Select all

#define ArrayInit2D(bar, tmp, x, y) { \
  ArrayInit(tmp, 0, y); \
  ArrayInit(bar, tmp, x);  \
}

task main(){
  int ibar[][], itmp[];
  float fbar[][], ftmp[];
  
  int i, j, imax, jmax;
  jmax=imax=122;
  
  ArrayInit2D(ibar, itmp, imax, jmax);
  for (i=0; i< imax; ++i)  {
    for (j=0; j< jmax; ++j)   {
      ibar[i][j]=(i+1)*(j+1);
      TextOut(0,48, "                ");
      NumOut(0,48,ibar[i][j]);
    }
  }




  ArrayInit2D(fbar, ftmp, 10, 10);
  for (i=0; i< 10; ++i)  {
    for (j=0; j< 10; ++j)   {
      fbar[i][j]=(i+1)*(j+1);
      TextOut(0,32, "                ");
      NumOut(0,32,fbar[i][j]);
    }
  }

  while (true) {}

}
Last edited by HaWe on 04 Jun 2011, 09:55, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 5 guests