4

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;
}
Dave
  • 7,555
  • 8
  • 46
  • 88
  • 1
    Look up "variable length arrays". – cnicutar Feb 20 '13 at 20:52
  • 1
    I'm trying to think of a reason a `std::vector dynamic_arr(sz);` would *not* work for you, and it isn't coming to me. Well, for `sz == 0` I suppose you'd be screwed, but so would your code be with a VLA, so.. yeah. nothing. – WhozCraig Feb 20 '13 at 20:58
  • The code I'm looking at uses `__attribute__(aligned(32))` on the array for optimization against the IPP libraries; thus the author preferred a fixed size array. – Dave Feb 20 '13 at 21:00

2 Answers2

8

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]

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • I'd go as far as saying that VLA's are just a bad idea in general (especially when created via user input), but that's not an absolute like your point is. – Ed S. Feb 20 '13 at 20:57
  • @EdS. I would have to agree. Code should be as standards compliant as possible. – juanchopanza Feb 20 '13 at 20:58
1

Standard C++ does not support the variable length arrays (VLAs) of C99 and g++.

In standard C++ you can use e.g. std::vector, like this (your code modified):

#include <iostream>
#include <vector>

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

    // This line
    std::vector<double> dynamic_arr( sz );    // Initialized to zeroes.

     //output the value just to use the array.
     std::cout<<dynamic_arr[0]<<std::endl;
}

Also, for strings you can use std::basic_string, usually via the typedefs std::string and std::wstring.

Generally the main problem is just the dynamic size, as above, and then std::vector and std::basic_string do the job nicely. However, sometimes the problem is efficiency, how to do extremely efficient stack allocation of a dynamic size array. Many C and C++ implementations support the non-standard function alloca for that, but unfortunately they differ greatly in how it handles failures. As far as I know there is no commonly available library solution for that either. But happily, the main usage that I know of (even though as mentioned alloca is available for a number of platforms) has been for string encoding translation in Windows, and that's less and less relevant as time goes on and Windows programs more and more are pure Unicode-oriented.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331