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
Code: Select all
int map[10][20];
//Now I want to set the 1st dimension to 30, and the second dimension to 45
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).
}
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); // 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.)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?
realloc()ate using ArrayInit(), the memory that the original array "points" to gets free()d, and new memory is malloc()ated.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) { }
}
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) {}
}
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) {}
}
Users browsing this forum: Semrush [Bot] and 13 guests