When memory is allocated by local variables in C

Since local variables are also called automatic variables and it is assumed that they will be allocated to memory at run time, when accessing the function.

int main(){ int a; // declaration return 0; } int main(){ int a[]; // compilation error, array_size missing return 0; } int main(){ int a[2]; // declaration, but can't work without array_size, // so at compile time it is checked! return 0; } 

My question is whether it is just a rule to provide array_size in a declaration in C, or is memory allocated at compile time for the array (still a local variable)

How it works?

An array is a variable according to K programming with K & R. pg no 161.

+4
source share
8 answers

When you declare a local variable, its size is known at compile time, but memory allocation occurs at runtime.

So, in your examples, an array without size is clearly a problem for the compiler, because it does not know what size should be included in the assembler code.

If you do not know the size of the array, you can always use pointer types and malloc / free or even alloca . The first two work on the heap, and alloca actually uses the stack.

A notable exception is static variables. The storage for them is distributed by compilation / link time and cannot be changed at runtime.

Examples:

 int main(int argc, const char *argv[]) { int a; // a is a sizeof(int) allocated on stack } int main(int argc, const char *argv[]) { int a[2]; // a is a sizeof(int)*2 allocated on stack } int main(int argc, const char *argv[]) { int *a; // a is a sizeof(int*) allocated on stack (pointer) a = alloca(sizeof(int)*4); // a points to an area with size of 4 integers // data is allocated on stack } int main(int argc, const char *argv[]) { static int a; // a is allocated in data segment, keeps the value } 
+6
source
 int main(){ int a[2]; return 0; } 

Here int a[2]; is the definition of a variable called a . a is an array of two int .

What happens in practice is that the compiler emits code to use the stack space for two adjacent int objects (maybe 8 bytes, but the size of the int corresponds to the implementation). This assumes, of course, that the object is not deleted by the optimizer, because you never use it.

Compiler error for int a[999999999]; due to some hard restriction performed by the compiler, because it knows (or assumes anyway), there will never be enough stack for this.

+1
source

As pointed out in the comments of sgar91, an array, for example, in your example, is allocated when the function is called and must be determined by size. If you need dynamic arrays, you must allocate memory on the heap.

 int *values = malloc(sizeof(int) * array_count); 
0
source

Automatic allocation occurs when you declare an automatic variable, such as a function argument or a local variable . The space for the automatic variable is allocated when you enter the compound statement containing the declaration, and is freed when you exit the compound statement.

0
source

That is, a point in C where the arrays and pointers do not match.

Take this example:

 int main(){ int a[5]; int * b = malloc(sizeof(int) * 5); printf("sizeof a = %d\n",sizeof a); printf("sizeof int[5] = %d\n",sizeof(int[5])); printf("sizeof b = %d\n",sizeof b); free(b); return 0; } 

This will return:

 sizeof a = 20 sizeof int[5] = 20 sizeof b = 4 

The variable a is internally declared as int [5], an integer pointer pointing to a block of memory with space for 5 integers.

0
source

For local variables, the memory they consume is on the stack. This means that they must have a fixed size, known at compile time, so when the function is called, the exact amount of memory needed is added to the stack by changing the value of the stack pointer. This is why the array must have a size: the number of bytes on the stack must change by a fixed amount when the function is called.

A call to malloc () and a similar allocation of memory from the heap; memory allocated in this way at runtime can be of variable size.

0
source

There is a difference between local and automatic variables in C. A local variable can be automatic or static, which determines whether the memory for it will be allocated on the stack or permanently when the program is first executed.

With this code:

 int main(){ int a[]; //compilation error, array_size missing return 0; } 

This is an incomplete array. The error is that the compiler does not know how many int programs will need to be allocated.

0
source

The size of the array must be known at compile time, and you can pass the size either directly to the array, or indirectly, since memory is determined at compile time, but allocated at runtime, except for a variable-sized array.

0
source

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


All Articles