-1

I always knew that it was not possible to build a dynamic array in C without using malloc and free, so why is this code compiling and running correctly?

#include <stdio.h>
#include <stdlib.h>


int main()
{
    int a;
    printf("Insert a number: ");
    scanf("%d", &a);

    int array[a];
    int i;
    for(i=0; i<a; i++)
    {
        array[i] = rand();
    }

    for(i=0; i<a; i++)
    {
        printf("%d\t", array[i]);
    }
    puts("");
    return 0;
}

I understand that this is not a really dynamic array since there is no way to change the size of "array" after it has been declared, nor it can be freed callling free() but still I always thought that the size of static array must be known at compile time which is clearly not the case here..

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
woggioni
  • 1,261
  • 1
  • 9
  • 19
  • 7
    It is known as a VLA(Variable Length Array). It was introduced in C99 – Spikatrix Dec 31 '14 at 09:54
  • What makes you think it is a static array? I can't see the "static" keyword anywhere. And the "things you always knew" are not always the things that are true, as explained by Cool Guy. – gnasher729 Dec 31 '14 at 09:57
  • as far as I know "static array" means an array which is allocated before the execution of the main(), which is the case of every fixed-sized array you use in your code – woggioni Dec 31 '14 at 10:00
  • 1
    @user2318607 : The array `array[]` is not allocated *before* main is executed, and it is not the case for *every fixed-sized array*. The `static` storage class specifier is not the *opposite* of *dynamic*. In this case `array[]` has `auto` storage class - it is allocated on the stack on entry to the function in which it is placed. In this case, because it is in the function `main()` it just happens to have a *similar* lifetime to a `static`. – Clifford Dec 31 '14 at 10:05
  • if i declare in a function a local variable like int a[5], isn't it allocated before execution on the stack and stays there for the whole lifetime of the process? this is what I mean with static array – woggioni Dec 31 '14 at 10:07
  • No. That function with `int a[5]` may never get called or it may recurse needing multiple `int a[5]`. The space is allocated at run time. – chux - Reinstate Monica Dec 31 '14 at 10:09
  • 2
    @CoolGuy : You really should post answers as answers rather than comments. That is clearly an answer. – Clifford Dec 31 '14 at 10:12
  • @Clifford ,I've already posted it....In the duplicate post! – Spikatrix Dec 31 '14 at 10:13
  • @CoolGuy Fair enough – Clifford Dec 31 '14 at 10:14

1 Answers1

1

What you are using is variable length array. Which is supported by C99 and latter. But note that VLA has automatic storage duration unlike dynamic memory allocated by malloc family functions.

Also note that compile time allocation is not equivalent to static array. static array and static allocation are different.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    *But note that its scope is limited to the function in which it is declared, unlike dynamic allocation.* you are mixing lexical scope and storage duration. – ouah Dec 31 '14 at 10:07