Comment by uecker
16 hours ago
Right. Also it might it sound like array-to-pointer decay is forced onto the programmer. Instead, you can take the address of an array just fine without letting it decay. The type then preserves the length.
16 hours ago
Right. Also it might it sound like array-to-pointer decay is forced onto the programmer. Instead, you can take the address of an array just fine without letting it decay. The type then preserves the length.
C: int foo(int a[]) { return a[5]; }
Oops.
D: int foo(int[] a) { return a[5]; }
Ah, Nirvana!
How to fix it for C:
https://www.digitalmars.com/articles/C-biggest-mistake.html
You need to take the address of the array instead of letting it decay and then size is encoded in the type:
Or for run-time length:
https://godbolt.org/z/dxx7TsKbK\*
Nice, when you know the length at compile time, which is rarely from my experience.
The holy grail is runtime access to the length, which means an array would have to be backed by something more elaborate.
Oh, it also work for runtime length:
https://godbolt.org/z/PnaWWcK9o
Now try that on a compiler without -fsanitize=bounds, yet full ISO C compliant.
1 reply →