|
|
|
|
|
by bsder
984 days ago
|
|
> Also note that most other languages except C++ also have VLAs. I don't think that's actually true. Lots of modern languages have "pointer plus length"--they call them lots of different things but I think "slices" is a common term. But those aren't VLAs. Some languages have variable vectors backed by a compile-time fixed-size array (See Zig: BoundedArray). But, again, not a VLA. I'm trying very hard to think of a non-GC language that allows you to allocate a run-time length array on the stack other than C, and I'm not coming up with one. |
|
C has variably modified types (in CS usually known as dependent types), where the length is encoded into the type. A VLA has a dependent type: char buf[n] or a pointer to a VLA has: char (*buf)[n]. This is super powerful and theoretically sound concept although not yet really exploited in C. But the bound travels with the type and you can get bounds checking at run-time:
char buf[n]; auto foo = &buf; buf[n] = 1; // run-time bounds check possible
Pascal, Ada, Fortran, D have VLAs and certainly more languages have VLAs.