Your question involves behavior not covered by the C++ standard, so some explanation of the rules involved is necessary.
You do not state which C++ implementation you are using, but the behavior reproduces with GCC, so this answer presumes you are using GCC, and I use GCC 11.1 for reference.
By default, GCC does not conform to the C++ standard when compiling C++ source files. Per the last sentence of clause 2.2 of the GCC 11.1 documentation, the default language it uses is gnu++17, which is the “C++17 standard with GNU extensions.” This is a bit of a misnomer as gnu++17 is not just an extension of the 2017 C++ standard but varies from it.
In C++, array dimensions should be constant expressions, and the standard requires a C++ implementation to diagnose when they are not. Given int t1 = 7; and int t2 = 5;, t1 and t2 are not constant expressions as the C++ standard defines them. In gnu++17 mode, GCC does not diagnose this violation of the rule, so it is deviating from the standard, not just extending it.
The C standard allows defining arrays with variable lengths, and it is not an uncommon extension for a compiler to allow this in C++. However, the C standard does not allow for initializing a variable-length array (in that it requires a diagnostic for it, although a C implementation is allowed to accept the initialization).
So the program you show is two steps removed from the C++ standard, one for allowing variable-length arrays and another for initializing them, and the standard does not tell us what should happen. Further, in GCC’s documentation on its extensions to C++, I do not see mention of variable-length arrays, let alone initializing them. The section on extensions to C contains some discussion of variable-length arrays but no mention of initializing them.
Thus the GCC documentation also appears silent on what should happen.
Testing various source code in GCC’s gnu++17 mode suggests:
- If you define a variable-length array with no initialization (no
= … after the declarator, just ;), GCC does not initialize it. (I saw non-zero data throughout the array.)
- If you define a variable-length array with some initializers (for example
= { 1, 2 }), GCC initializes the elements corresponding to the initializers but not other elements.
- If you define a variable-length array with an empty initializer list (
= {}), GCC initializes the array to zero.
- If you define the inner array dimension with a constant expression (for example, changing the
t2 definition to const int t2 = 5;) and give some initializers, GCC initializes the entire array, using zeros for the elements without initializers. (I suspect this would be so for multidimensional arrays when all dimensions other than the outermost are constant expressions.) Thus, GCC initializes the fixed-length inner arrays even though there are a variable number of them.
In those results, I do not see evidence of an intended pattern. I suspect the behavior here has been overlooked by GCC developers. That is, I do not think the behavior in these various cases all result from deliberate decisions. It may be that initialization of a variable-length array initializes only those elements that are explicitly initialized (taking {} to mean all elements are initialized), leaving the others uninitialized (unlike initialization of fixed-length arrays).
I do not see the behavior you report with double; when I change float to double, I still see uninitialized elements containing non-zero values.
float arr[t1][t2] = {0}wheret1andt2are variables is a (two-dimensional) variable-length array (VLA). VLAs are not valid in standard C++ - irrespective of how you try to initialise them.error: variable-sized object may not be initialized.arr[t1][t2]is invalid