1

I want to define a 2D array statically. The size of the array is determined by a variable. How do I do this? I do not want to dynamically define the array. I heard that there is a way to do this.

Namratha
  • 16,630
  • 27
  • 90
  • 125

5 Answers5

3

Answer is No You Cannot in C++.
Dimensions of the array must be known at compile time.

int my_array[6][7];   // okay  
int my_array[H][7];   // ISO C++ forbids variable length array 
int my_array[6][W];   // ISO C++ forbids variable length array 
int my_array[H][W];   // ISO C++ forbids variable length array 

Some compilers do support Variable Length Arrays(VLA) through their own extension but VLA are not defined in the C++ standard, hence using VLA will be non conforming to C++ standard.

VLA's were introduced in C99 C standard. C++ was branched out from C standard of C98. By the time C introduced VLA, C++ already had Vectors and had no need to support or encourage VLA. Hence, VLA was never formally accepted in the C++ Standard, some C++ compiler still support VLA through compiler extensions.

Since, you tagged your Q C as well as C++, to summarize the answer:

In C99 & versions after that : You Can  
Versions before C99: You Can't
In C++(Any version): You can(through compiler extensions) but You should'nt

Here is the legendary C++ FAQ which explains everything about arrays.
I learned a lot from it. :)

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    Hm, these array declarations seem [oddly familiar](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810676#4810676)... ;-) – fredoverflow May 03 '11 at 06:08
  • @FredOverflow: Added the link in answer. Apologies for the delay in adding that, had a sprint meet :( – Alok Save May 03 '11 at 06:46
1

I dont think so you can statically define it! However you can use vector but underneath it too does dynamic allocation for you

koool
  • 15,157
  • 11
  • 45
  • 60
1

If one of the dimensions of the array is variable, then the size must be variable, then it must be dynamically sized - or sized in such a way that the the array is statically sized larger than the largest value that the variable could be.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
0

By "variable", I assume you're talking about a non-constant value.

In C99, yes:

int size = ...;
int array[size][size];

In C++, you can't. The alternative is to use pointers and dynamic allocation (or better yet, vectors).

In C versions prior to C99, it's also impossible. Use malloc().

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • I'm assuming that internally this is variable sized on the stack? – Michael Dorgan May 03 '11 at 04:02
  • @Michael: You mean the array is allocated on the stack? – Namratha May 03 '11 at 04:29
  • @Namratha: Unless you do a `new` or `malloc` to allocate memory dynamically, objects are created on stack. Also, `new` and `malloc` are used to allocate dynamic memory on heap and point the `pointer`to it. `Arrays are NOT pointers` & Yes they are allocated on stack. – Alok Save May 03 '11 at 04:50
0

This depends where and when the variable is initialized. If its something done at compile time you can use templates to get it done: template </* args */> struct ConstExpr{enum{value = /* math goes here */};};, else its impossible without selfmodifying code(and I'm pretty sure this is gonna very dangerous, because you'll need to alter the PE so you can somehow get your allocated space change before its virtualized).

Necrolis
  • 25,836
  • 3
  • 63
  • 101