When is memory allocated to local variables in C

流过昼夜 提交于 2019-12-01 04:45:13

When you declare local variable, the size of it is known at a compile time, but memory allocation occurs during execution time.

So in your examples, array without a size is clearly a problem to compiler, as it doesn't know what is the size to include into assembler code.

If you don't know the size of an array, you can always use pointer types and malloc/free or even alloca. The first two operate on heap, and alloca actually uses stack.

The notable exception is static variables. The storage for them is allocated at a compile/link time already and can't 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
}
int main(){
    int a[2];
    return 0;
}

Here, int a[2]; is a definition of a variable named a. a is an array of two int.

What happens in practice is that the compiler emits code to use space on the stack for 2 adjacent int objects (probably 8 bytes, but the size of int is up to the implementation). That's assuming of course that the object isn't removed by the optimizer because you never use it.

The compiler error you got for int a[999999999]; is due to some hard limit enforced by the compiler, because it knows (or anyway assumes) there will never be enough stack for that.

There's a difference between local and automatic variables in C. A local variable may be automatic or static, which determines whether the memory for it is 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 because the compiler doesn't know how many ints the program will need to allocate.

As sgar91 stated in the comments, an array such as in your example is allocated when a function is called and must be determinate in size. If you need dynamic arrays, you must allocate the memory on the heap.

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

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

Thats the point in C, where arrays and pointers aren't the same.

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 internal declared as int[5], an integer pointer pointing to a memory block with space for 5 integers.

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 that 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.

Calls to malloc() and similar allocate memory from the heap; memory allocated in this way at runtime can be of variable size.

Array size should be known at the compile time and you can pass size either directly to the array or indirectly as memory is decided at compile time but allocated at run time, except for the variable sized array.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!