The source code as follows:
using namespace std;
int main()
{
int m = 4;
int arr[m] = {1, 2, 3, 4};
printf("%d\n", arr[2]);
return 0;
}
When I compile with g++, it compiles successfully as an executable. But when I compile with clang++, I get the following error:
VLAs.cpp:8:10: error: variable-sized object may not be initialized
int arr[m] = {1, 2, 3, 4};
^
1 error generated.
After testing, I found that clang ++ can support the definition of VLBs(int arr[m];
), but does not support defining VLBs while initializing them. I would like to know the root cause of this difference