Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic array without new operator? [duplicate]

Since when is this valid syntax? (works using g++ 4.6.3) What should I search on to find further information about this (I'm used to new/delete)?

#include <iostream>

int main(){
    size_t sz;
    std::cout<<"number?\n";
    std::cin>>sz;

    // This line
    float dynamic_arr[sz];

     //output the (uninitialized) value just to use the array.
     std::cout<<dynamic_arr[0]<<std::endl;
     return 0;
}
like image 476
Dave Avatar asked Sep 05 '25 03:09

Dave


1 Answers

Varialbe length arrays (VLAs) are not standard C++, they are a compiler extension. You shouldn't use it if you want your code to be portable.

If you compile with the -Wvla -Werror flags, or -Werror=vla, your code will produce the error

error: variable length array 'dynamic_arr' is used [-Werror=vla]

like image 94
juanchopanza Avatar answered Sep 07 '25 22:09

juanchopanza