Search code examples
c++memory-managementvariable-length-array

dynamic and static memory allocation?


  int x;
    cin>>x;
    int arr[x];

The code must not compile because the program will try allocate a unknown memory for the array on the stack, BUT IT COMPILES! i know what dynamic memory is, i've read a lot about this but i don't understand , why does the program above runs?! shouldn't it be this way? :

int x;
cin>>x;
int *arr=new arr[x];

could someone plz give me an example in which does not work with static allocating and works only with dynamic allocating?


Solution

  • Some compilers may enable using dynamic size for arrays allocated from stack. It's not standard C++ though.