c: dynamic alloc of 3d array when 1 dimension unknown -



c: dynamic alloc of 3d array when 1 dimension unknown -

i trying dynamically allocate 3d array, these params: 1) first 2 dimensions know, , can defined constants (acount , bcount). 3rd needs decided @ runtime. 2) want able address array using: arr[i][j][k] = n; 3) want avoid death million+ mallocs

so in next code, first part using "*arr" works great, , 2nd part using "*brr" meets sorry end. there way accomplish these params using magic potion of asterisks ?

(i compiling using vs2010 c++, nasty castings.)

#define acount 9000 #define bcount 195 #define ccount 8 short (*arr)[bcount][ccount]; short (*brr)[acount][bcount]; void main(void) { arr = (short (*)[bcount][ccount] ) malloc( (unsigned long) acount * bcount *ccount * sizeof(short)); (int j = 0; j < acount; ++j) (int k = 0; k < bcount; ++k) (int m = 0; m < ccount; ++m) arr[j][k][m] = j + k + m; // still live here brr = (short (*)[acount][bcount] ) malloc( (unsigned long) acount * bcount *ccount * sizeof(short)); (int j = 0; j < acount; ++j) (int k = 0; k < bcount; ++k) (int m = 0; m < ccount; ++m) brr[j][k][m] = j + k + m; // error: unhandled exception ...access violation... etc }

apologies, realized after posted aversion excessive malloc (or calloc) statements. code uses excessive numbers of statement, require excessive cleanup (free statement). leave post in case can utilize part of it. ryk

here code illustrative dynamically creating , allocating space matrices. show pattern successively larger dimensioned matrices. note, argument list includes desired size (order) of each index; i.e. row (r), c,r (column, row) p,c,r (page, column, row) , on. note these functions replace malloc, calloc, preference. regards, ryk

double * create1d(int r) { double *space; double *arr; space = calloc(r*sizeof(double), sizeof(double)); arr = calloc(sizeof(double), sizeof(double)); arr = (double *)space; homecoming arr; } double ** create2d(int c, int r) { double *space; double **arr; int y; space = calloc(c*r*sizeof(double), sizeof(double)); arr = calloc(c * sizeof(double *), sizeof(double)); for(y=0;y<c;y++) { arr[y] = (double *)space + (y*r); } homecoming arr; } double *** create3d(int p, int c, int r) { double *space; double ***arr; int x,y; space = calloc (p*c*r*sizeof(double),sizeof(double)); arr = calloc(p * sizeof(double **), sizeof(double)); for(x = 0; x < p; x++) { arr[x] = calloc(c * sizeof(double *),sizeof(double)); for(y = 0; y < c; y++) { arr[x][y] = ((double *)space + (x*(c*r) + y*r)); } } homecoming arr; } double **** create4d(int hr, int p, int c, int r) { double *space; double ****arr; int w,x,y; space = calloc(hr*p*c*r*sizeof(double), sizeof(double)); arr = calloc(hr * sizeof(double ***), sizeof(double)); for(w=0;w<hr;w++) { arr[w] = calloc(p * sizeof(double **), sizeof(double)); for(x=0;x<p;x++) { arr[w][x] = calloc(c * sizeof(double *), sizeof(double)); for(y=0;y<c;y++) { arr[w][x][y] = ((double *)space + (w*(p*c*r) + x*(c*r) + y*r)); } } } homecoming arr; } double ***** create5d(int hc, int hr, int p, int c, int r) { double *space; double *****arr; int v,w,x,y; space = calloc(hc*hr*p*c*r*sizeof(double),sizeof(double)); arr = calloc(hc * sizeof(double ****),sizeof(double)); for(v=0;v<hc;v++) { arr[v] = calloc(hr * sizeof(double ***),sizeof(double)); for(w=0;w<hr;w++) { arr[v][w] = calloc(p * sizeof(double **),sizeof(double)); for(x=0;x<p;x++) { arr[v][w][x] = calloc(c * sizeof(double *),sizeof(double)); for(y=0;y<c;y++) { arr[v][w][x][y] = ((double *)space + (v*(hr*p*c*r) + w*(p*c*r) + x*(c*r) + y*r)); } } } } homecoming arr; } double ****** create6d(int hp, int hc, int hr, int p, int c, int r) { double *space; double ******arr; int u,v,w,x,y; space = calloc(hp*hc*hr*p*c*r*sizeof(double),sizeof(double)); arr = calloc(hp * sizeof(double *****),sizeof(double)); for(u=0;u<hp;u++) { arr[u] = calloc(hc * sizeof(double ****),sizeof(double)); for(v=0;v<hc;v++) { arr[u][v] = calloc(hr * sizeof(double ***),sizeof(double)); for(w=0;w<hr;w++) { arr[u][v][w] = calloc(p * sizeof(double **),sizeof(double)); for(x=0;x<p;x++) { arr[u][v][w][x] = calloc(c * sizeof(double *),sizeof(double)); for(y=0;y<c;y++) { arr[u][v][w][x][y] = ((double *)space + (u*(hc*hr*p*c*r) + v*(hr*p*c*r) + w*(p*c*r) + x*(c*r) + y*r)); } } } } } homecoming arr; }

c multidimensional-array malloc

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -