5 ms·
> Unfortunately working with dynamically-allocated buffers is still a thing; adding array syntax just favors one form of size specification without solving the
by WalterBright 6y ago
> Unfortunately working with dynamically-allocated buffers is still a thing; adding array syntax just favors one form of size specification without solving the other case.
All that is needed is a mechanism for forming a fat pointer from a pointer and a length. In D this looks like:
int* p = cast(int*)malloc(length * sizeof(int));
if (!p) fatalError();
int[] a = p[0 .. length];
...
int x = a[length + 1]; // runtime error: buffer overflow
In C, this could be done via a macro with no additional core language changes.
- akira2501 6y agoTongue in cheek: I already have a fat-pointer: struct foo { int a[10]; } f; func(&f);