Page 1 of 3
[NXC] ArrayInit for 2D arrays
Posted: 03 Jun 2011, 19:58
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
Re: [NXC] ArrayInit for 2D arrays
Posted: 03 Jun 2011, 20:22
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)
Re: [NXC] ArrayInit for 2D arrays
Posted: 03 Jun 2011, 20:26
by nxtboyiii
But it is possible to resize a 1D variable!!!
Re: [NXC] ArrayInit for 2D arrays
Posted: 03 Jun 2011, 20:31
by HaWe
?
show me an example, please...
Re: [NXC] ArrayInit for 2D arrays
Posted: 03 Jun 2011, 20:38
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
Re: [NXC] ArrayInit for 2D arrays
Posted: 03 Jun 2011, 21:45
by HaWe
amazing...
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); //
Re: [NXC] ArrayInit for 2D arrays
Posted: 04 Jun 2011, 01:21
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.
Re: [NXC] ArrayInit for 2D arrays
Posted: 04 Jun 2011, 09:26
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) { }
}
Re: [NXC] ArrayInit for 2D arrays
Posted: 04 Jun 2011, 09:39
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) {}
}
Re: [NXC] ArrayInit for 2D arrays
Posted: 04 Jun 2011, 09:40
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) {}
}