Why does the top code work and the bottom code not in c++ for dynamic matrix allocation? -
i've gotten rid of codes work distracting. i'm trying allocate single block of memory 3d array. allocation jagged array, it's more efficient contiguous block.
here's current code snippet:
//creating array of proper size //i first create array of pointers phi = new double**[xlength]; //i create array of pointers phi[0] = new double*[xlength*ylength]; //at point, assume phi[0] , phi[0][0] have same location. not true? phi[0][0] = new double[xlength*ylength*tlength]; //now, allocate big block of data store data //i assume phi[0][0][0] has same location phi[0][0]. right? //now, i'm trying setup pointers match in single block of memory (int i=0;i<xlength;i++) { (int j=0;j<ylength;j++) { phi[i][j] = phi[0][0] + tlength*ylength*i + tlength*j; } } //does not work? feels though should adding working code since answer depends on working code
double*** = new double**[m]; double** b = new double*[m*n]; double* c = new double[m*n*o]; (int i=0; i<m; i++) { (int j=0; j<n; j++) { b[n*i+j] = c + (n*i+j)*o; } a[i] = b + n*i; } (int i=0; i<m; i++) { (int j=0; j<n; j++) { (int k=0; k<o; k++) { a[i][j][k] = <my_value>; } } here's solution guys!
don't 3d matrix, lol! unpopular post ever!
do 1d matrix , follow answer on link c++ segmentation fault after when trying write matrix
this important part of link:
but cannot use phi[i][j][k] notation, should
#define inphi(i,j,k) phi[(i)*xlength*ylength+(j)*xlength+(k)] and write inphi(i,j,k) instead of phi[i][j][k]
remarks:
you not free allocated memory using delete[]. that's very bad practice. always should remember free memory allocated.
it very, very uncomfortable choose jagged arrays (arrays of arrays) long term use. lot easier solution allocate single array:
double * phi = new double[xlength * ylength];and access (x, y)-th element following:
phy[y * xlength + x] = 20.0;the allocation takes less time, have lot less things free (only phi itself) , access time more-less equally fast.
consider using
std::vectororstd::array. since use c++ , not c, natural way use these containers instead of raw arrays, lot more managable , care free contents if allocated statically. in case like:#include <vector> (...) std::vector<std::vector<double> > phi; phi.resize(xlength); (int x = 0; x < xlength; x++) phi[x].resize(ylength);
solution:
your solution won't work. reason, why author of original code uses 3 variables is, 1 of them has contain actual data , 2 others serve pointers parts of original data.
in case, try keep both data , pointers parts of data in same array, cannot work. if want [][][] notation, have allocate jagged arrays in nested loops, i've shown in solution. on other hand, if want 1 array keep single block of data , keep pointers, you'll have author of first piece of code did.
it took me few minutes of time figure out, how three-dimensional solution 3 variables work, i'll leave explanation everyone, who'll encounter thread.
the general idea have 1 variable actual data , 2 proxy variables set of pointers, allows addressing actual data [][][] notation.
c contains actual data, of size [zdim * ydim * xdim]. can access (x, y, z) element addressing [z * xdim * ydim + y * xdim + x]. [][][] notation means, data organized slices (z), contains rows (y), contains elements(x).
you construct array b containing pointers all rows, ordered slice. b contains: (slice 0, row 0), (slice 0, row 1), ..., (slice 0, row ydim - 1), (slice 1, row 0), ...
then, construct array containing pointers elements of array b, such z-th element of points (z * ydim) = 0-th row of z-th slice. finally, when address array a, works following:
a[z]contains address element of array b containing pointer 0th row of zth slicea[z][y]moves pointer, such have actual pointer 0-th element of y-th row of z-th slice.- finally,
a[z][y][x]shifts x elements, such receive x-th element of y-th row of z-th slice.
now should clear, why need additional variables , why cannot done single variable.
bottomline: never use such solution, huge waste of memory. instead, flatten arrays , address [z * xdim * ydim + y * xdim + x] or use jagged std::vectors or std::arrays.
Comments
Post a Comment