|
|
|
|
|
by uecker
987 days ago
|
|
VLA were not removed. VLAs are almost always better than the next best alternative:
- They are better than alloca due to proper scoping and standard compliance.
- They use less stack than regular arrays on the stack with a worst-case size (e.g. a divide-and-conquer algorithm that may need O(N^2) stack space without VLAs could potentially be written using O(N log(N)))
- VLAs can allow more accurate bounds checking than with worst-case sized arrays.
- VLAs are faster than heap allocation. There are issues with VLAs: If the size is controlled by an attacker, then this could cause security issues. This is largely mitigated by -fstack-clash-protection which transforms this into a DOS (same as unbounded heap allocation) and you want to have stack clash protection anyway. Static analysis tools and compiler flags can also help to detect cases where the size is controlled by input from the network. Assembler for VLAs worse than for fixed size array, but this goes at the cost of less space saving. But, again, people who avoid VLAs blindly because of these issues then often use something which is worse. Also note that most other languages except C++ also have VLAs. |
|
They were made optional in C11, which for real-world purposes makes puts it on the same level as a compiler-specific language extension. For instance MSVC will (most likely) never support VLAs:
https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...
(specifically: https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-...)