简体   繁体   中英

How to convert one-dimensional array into two-dimensional without memory allocating?

I have a filled one-dimensional array double *vals as a class component with sizes Nn[0]*Nn[1] . I need to get 2-dimensional array **w (w[Nn[1]][Nn[0]]) without allocating new memory for it, eg i need to represent vals as 2-dimensional array.

Using g++ compiler i can make

double (* w)[Nn[0]] = (double (*)[Nn[0]])val;

But VS compiler or intel compiler don't allow to use non-constant expression as dimension array.

In general, I can just use element in initial vals array converting 2 int indices (i,j) of w[i][j] element into global index and do not declare w at all. But it would be great if it's possible to get 2-dimensional array w on initial memory (with compiling with intel compiler too). So is there any way to do it?

If wals is a class, you can implement your access operator to function as if it were 2D array.

walsdataType walsclass::operator()(int i, int j){return walsdata[i*N+j]};

with walsdata being the class member for storing data and N being the row length. You should do the bound checking as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM