12
  1. this can work:

    const int size = 2;
    int array[size] = {0}; 
    
  2. this has compile error:

    int a = 2;
    const int size = a;
    int array[size] = {0};
    

why?

Ello
  • 281
  • 1
  • 3
  • 12

4 Answers4

16

Because the guys in the C++ committee decided so.

The technical reason is that the first expression that is used to initialize size is a constant expression and it can be computed during compilation. This means that the compiler can also know how big the array is going to be and the allocation (in this case "reservation" may be a more appropriate term) can be done at compile time.

In the second case instead the expression is not a constant expression (given the C++ definition) and this revervation is not possible.

The fact that in the second case the value is indeed fixed by the time size is initialized is totally irrelevant. The rules are base on the "kind of expression" and the second expression uses mutable variables and thus the compiler considers it non-constant.

Allowing the second form for compile-time initialization would require a flow analysis because the compiler would need to distinguish between

int a = 2;
const int size = a;

and

int a = foo();
const int size = a;

where the expression involving size is indeed identical.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 1
    So there's no way to initialize an array with a known size if this size isn't initialized like that? – Zap Oct 25 '18 at 19:52
  • 1
    @Zap you can inizialize an array in the stack memory only if the size value is known at static time, otherwise you can do it using the heap memory. – EduBic Dec 23 '21 at 13:20
8
const int size = 2;
int array[size] = {0}; 

Here, 2 is a literal value, that means, you can't change it, and the compiler know the value at compile-time.

int a = 2;
const int size = a;
int array[size] = {0};

However, a is a variable, which means that the value a could be changed, and will be sure at run-time So the compiler forbids you. You can use

int a = 2;
int size = a;
int* array = new int[size];

thus, you can apply for an array with dynamic size.

YaleCheung
  • 630
  • 3
  • 9
4

It's about to memory management.

When OS trying to run your program because of nature of C++, OS gonna want know exact space for stack area. In first example OS is gonna know the value of variable is not gonna change. But for second example at view of OS your first variable which is "a" can be change between

int a = 2;

this and this

const int size = a;

your variable a can be changeable. That's why your compiler don't let you compile your code.

In order to learn more about memory management basics. I recommend you to https://stackoverflow.com/a/24922/2326288 this comment.

Community
  • 1
  • 1
mtilhan
  • 258
  • 4
  • 12
0

Because in the first case, size is initialized by the compiler at compilation time. In the second case a may be initialized at run-time and so size will also be initialized at run-time and is no longer a compile-time constant.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621