1

I've got a class and I'm going to declare the size of the array (two dimensional) based on input from a user. so :

class myClass {/*...*/}

int main(){
myClass* arrayObj = new myClass[100][100];

That works fine, and it should put the array on the heap. But I need to do :

int arraySize;
cin >> arraySize;
myClass* arrayObj = new myClass[arraySize][arraySize];

I am getting the error : "arraySize" cannot appear in a constant-expression.

I'm assuming that means that I can only have constants in the declaration of the array, but if not, then how can I do it? The array is too big to fit on the stack, that is why I am doing it on the heap in the first place.

Edit : I've got it working with the pointers, but I'm having another problem, I have a function that is using the array, ie.

void myFunction() 
{
    /*...*/
    arrayObj[something][something].variable = somethingElse // error here
}

int main ()
{ 
     /*...*/
    int arraySize;
    cin >> arraySize;
    MyClass **arrayObj = new MyClass*[arraySize]
    for (int i = 0; i < arraySize; i++) arrayObj[i] = new MyClass[arraySize]
    /*...*/
}

I'm getting : error: 'arrayObj' was not declared in this scope. I can see why, but it's on the heap and it's a pointer, shouldn't it be global? If not, how would I make it global?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    This is higly relevant: http://stackoverflow.com/questions/1887097/variable-length-arrays-in-c – supertopi Dec 01 '13 at 19:54
  • 2
    You should take a look at [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) – Some programmer dude Dec 01 '13 at 19:54
  • "That works fine..." - no, it doesn't. Your compiler is being awfully lenient. You're assigning a `myClass (*)[100]` to a pointer of type `myClass *`. They're not the same type, and a heightened warning level from your compiler will likely tell you that (and fail with error if warnings are configured to fail, but it *should* be an error regardless and I'd consider scrapping your toolchain if it isn't). That should also tell you something about why your second snippet will also fail. – WhozCraig Dec 01 '13 at 19:58

3 Answers3

2

First of all you are mistaken saying that this

class myClass {/*...*/}

int main(){
myClass* arrayObj = new myClass[100][100];

works fine. The compiler shall issue an error because there is no implicit conversion from myClass ( * )[100] to myClass *

As for your problem then you should use the following approach:

myClass **arrayObj = new myClass *[arraySize];

for ( int  = 0; i < arraySize; i++ ) arrayObj[i] = new myClass[arraySize];
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

C++ doesn't really have a built-in model of variable sized multi-dimensional arrays. Only the outermost dimension can vary at run-time, all other dimensions are fixed. The background is how C++ does address arithmetic: when adding an offset to a pointer it is advanced by the size of an object with a statically determined size.

If you want to have a multi-dimensional array varying in other dimensions, you'll need to use a suitable class or implement one yourself (the standard C++ library has std::valarray<T> to sort of deal with multi-dimensional arrays but their use is, let say, not entirely straight forward). The easiest approach is probably to use a std::vector<std::vector<myClass> >.

A more efficient approach is to allocate a large std::vector<myClass> as a member of a class and have operator[]() for this class return a view to a corresponding section of this array. For starters I would probably just use a std::vector<std::vector<myClass> > wrapped into a class and change the implementation if it turns out to be too inefficient.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
0

If you must use arrays, then another way to get around this is impose a limit on the number of elements in the array and make sure that this limit is enforced in your code. This is one of the disadvantages of using arrays vs. std::vectors. Arrays are a fixed size while vectors can keep growing dynamically. By the way, what do you mean by "The array is too big to fit on the stack, that is why I am doing it on the heap in the first place."? If it is too big to fit on the stack, then maybe we should look at why the array is so large in the first place. Maybe there is a better way to solve whatever problem you're trying to deal with.

Andrew
  • 1,581
  • 3
  • 18
  • 31
  • It's the "game of life", the user inputs a grid size, the program seems to crash with any input greater than about 300 (remember, that's squared so it's a fairly large array). – user3055201 Dec 01 '13 at 20:59