0

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, e.g. 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?

4
  • 4
    C++ does not allow non-constant expressions as array dimensions, while modern C allows it. So pick one language for the question, instead of two. Commented Dec 7, 2015 at 7:21
  • Ok, i deleted C from tags. It's C++ code Commented Dec 7, 2015 at 7:23
  • double *vals is not an array. It's a pointer. Commented Dec 7, 2015 at 13:40
  • What you're describing is the concept of a view into another container. In your case the container is a 1-dimensional array and the view presents it to the caller as a 2-dimensional array. One such implementation of a view is provided by the Boost.MultiArray library. Commented Dec 10, 2015 at 22:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What operator is that supposed to be?
That is the access operator. You use it to access the data stored in your member walsdata like this: walsinstance(i,j)
I don't know any access operator in c++. Can you be more specific? Did you mean operator()(int i, intj)?
Yes, since you will be making your own class (I've named it walsclass but you can name it whatever you prefer) and you should implement your access operator, namely operator()(int i, int j). I've also realized that I've made a typo and will correct my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.