0

I have a variable in class declared as compile-time constant with known size:

static const int array[5][5]; // constants initlialised in another place

And a function that returns it virtually:

virtual const int** getArray() { return array; }

How to get this array with this method, and cast it to fixed-size array, not pointers-based, so I can use it like cout << data[2][2] ?

Sample that dosn't compile:

const int[5][5] data = object->getArray();
cout << data[2][2]; 

Sample that compiles but crashes application:

const int** data = object->getArray();
cout << data[2][2]; 

Note: one solution is to create typedef arr[5] and declare methods with arr* but i don't want to create a typedef for each compile-time size wich I use like typedef arr5[5]; typedef arr10[10] etc. I'm looking for something more like:

const int(*)[5] data = object->getArray(); // won't compile, example only

Let's assume that compile-time constant array is loaded with dynamic DLL and is already in memory, is it possible to use this data as array without allocating new memory and populating it from compile-time constants?

2
  • Like I said in the previous question, const int** is not the same as a pointer to an array with two dimensions. It doesn't work like that. You can't cast it. You could cast a const int* to const arr * if you wanted, where arr is a typedef. Commented May 7, 2013 at 8:44
  • The getArray function makes no sense and isn't valid C++. Commented May 7, 2013 at 8:44

4 Answers 4

2
virtual const int** getArray() { return array; }

That won't work, and shouldn't compile; you're trying to return a pointer to some pointers, but there are no pointers to point to, only an array of arrays.

You can return a pointer to those arrays, preserving the type:

virtual const int (*getArray())[5] { return array; }
const int (*data)[5] = getArray();
cout << data[2][2];

In C++11, it might be nicer to wrap the arrays up in std::array, and return a reference to that, to avoid some of the nasty syntax.

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

Comments

1

Use std::array:

#include <array>

static const std::array<std::array<int, 5>, 5> myarray; // constants initlialised in another place

virtual const std::array<std::array<int, 5>, 5>& getArray() const 
{ 
  return myarray; 
}

If you do not have C++11 support, you can use std::tr1::array or boost::array (or roll out your own fixed size array type.)

2 Comments

Is this a full compile-time solution? If my original array takes 10MB of memory, will this solution take 10MB or 20MB? (allocating dynamic 10MB and initialising it from 10MB constants). We talk about dynamic DLL environment and let's assume that all compile-time constants are also in memory!
@killer_PL it will have the same size as a 2D array. There is no dynamic memory allocation. It should be the same as your static 2D array example.
0

that is not possible. You lose all size information when an array is passed to or returned from a function. The array decays into a pointer so to speak

1 Comment

Ok I lost it, but remeber that I know it at compile time - can I use this information to cast back this array (like "force dimensions")?
0

It's simple enough, you are almost there

const int(*)[5] data = reinterpret_cast<int(*)[5]>(object->getArray());

2 Comments

This doesn't want to compile: error: expected primary-expression before 'const', error: expected ';' before 'const' (compiling with G++)
The error says before 'const', that would seem to indicate there is something wrong on the preceding line.

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.