Difference between providing array size at runtime and dynamic memory allocation

In the following code, I provide the size of the array at runtime.

#include <stdio.h>

int main()
{
    int i;
    scanf("%d",&i);
    int a[i];
}

Please tell me the difference between the code above and the code with malloc(). I know that array storage is on the stack, and dynamic memory ( malloc, callocetc.) is on the heap. So does my code work like malloc? If not, explain.

+4
source share
2 answers

In addition to your code, VLA is used, which:

  • did not exist until C99
  • were introduced as a new feature in C99
  • are now an optional function since C11

, (VLA ) , , , , - , .

+1

Above C99, :

int a[i] - , STACK HEAP ", malloc", , , - MAX_INT STACK (: ), .

0

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


All Articles