3 ms·
Yes, pointer + plus length is something else. C has variably modified types (in CS usually known as dependent types), where the length is encoded into the type
by uecker 3y ago
Yes, pointer + plus length is something else.
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.
- bsder 3y agoI don't think D has VLAs. Between the fact that most modern languages don't have VLAs and that the languages you mention are most certainly not modern, your statement of: "Also note that most other languages except C++ also have VLAs." is not even close to correct.
- uecker 3y agoD has dynamic arrays which can be on the stack. At least it looks to me like this: https://godbolt.org/z/vTqMah569 https://godbolt.org/z/vTqMah569 I guess this depends on whether you count higher level languages with GC which often have some kind of automatically managed dynamic array / vector type but not necessarily call them VLAs. I also did not say "modern" - not that this is clearly defined. If you only count Zig and Rust as modern, then those two do not have VLAs as far as I know. But this was not my main point anyway.