Why can't the size of a static array be variable?

Possible duplicate:
you can specify the size of the static array of the variable

I define an array in one of the child files as follows.

static int arr[siz]; 

Here siz is a global variable available for the child file. But the gcc compiler generates the following error:

 <filename>: <line_num> : error : storage size of 'arr' isn't constant 

Why can't I define an array of static variable?

EDIT: This seems to be a problem only for the static int type. If I change the type of the arr variable from static int to int , the error will disappear, although the size of the array still depends on the siz variable.

+6
source share
4 answers

Since the size of the declared array is not constant, you have a Variable Length Array (VLA) . VLAs are allowed by the c99 standard, but there are some limitations associated with it. You cannot have a variable length array with a static or extern storage class specifier.

You have a VLA with a static storage specification, and it is not allowed by the C99 standard.

Link:

c99 Standard: 6.7.5.2/8

EXAMPLE 4 All declarations of types with variable modification (VM) should be both in the block area and in the scope of the prototype. Array objects declared with a static or external storage class specification cannot be of variable length type (VLA) . However, an object declared with a static storage class specification may have a virtual machine type (that is, a pointer to a VLA type). Finally, all identifiers declared with a VM type must be regular identifiers and therefore cannot be members of structures or associations.

So, if you need a dynamic-sized array with a static storage specifier, you will have to use a dynamic array allocated to the heap.

 #define MAX_SIZE 256 static int* gArr; gArr = malloc(MAX_SIZE * sizeof(int)); 

EDIT:
To answer your updated question:
When you remove the static from the declaration, the storage specifier of the declared array changes from static to global, pay attention to the standard quote above, it clearly indicates the limitation that the VLA is not allowed using the static and extern storage specification. Obviously, you are allowed to have a VLA with a global repository specification, which was after removing the static .

+8
source

You allocate the array at compile time, so the compiler must know the size of the array in advance. You must declare siz as a constant expression before declaring arr , for example:

 #define siz 5 

or

 enum ESizes { siz = 5 }; 

Alternatively, if you need to determine its size at runtime, you can allocate it on the heap using malloc :

 static int* arr; arr = (int*)malloc(siz * sizeof(int)) 

EDIT : as eddieantonio pointed out, my answer is correct for C89. In C99, it is allowed to declare variable arrays.

+2
source

You cannot define any array of variable sizes. This is because arr[siz] forces the compiler (!) To allocate memory for your array (well, the compiler creates a program that ... but does not go into details). However, variables can be changed at runtime (!), Which means that the compiler is not able to find out how much memory is being allocated.

What you can do is

 static int* arr; arr = (int*) calloc(siz,sizeof(int)) 

These lines lead to a program that allocates memory at run time, so its size can also be determined at run time.

+1
source

You cannot declare an array of variable size static because its space is allocated in the data segment (or the bss segment in the case of an uninitialized variable). Therefore, the compiler must know the size at compile time and will complain if size is not a constant.

The main reason for this is that the size of the data segment contributes to the size of the generated executable, which is obviously created at compile time and therefore needs to be fixed.

0
source

Source: https://habr.com/ru/post/916205/


All Articles