3 ms·
I like how this is going, but I'm missing a few things here: > We can turn a pointer into a bounds checked array by "slicing" it: > int *p = (int*) malloc
by lelanthran 4y ago
I like how this is going, but I'm missing a few things here:
> We can turn a pointer into a bounds checked array by "slicing" it:
> int *p = (int*) malloc(10);
> int a[..] = p[0 .. 10];
When `p` is a parameter in a function, the function cannot know that it can create a slice of up to 10 elements (I assume that the `p[0 .. 10]` creates an array indexed from 0 - 9).
What if the line was:
int a[..] = p[0..12]
Do we still get undefined behaviour?
> A bounds checked array can be turned into a pointer:
> int *p = &a[3]; // point to 3rd element of a[..]
Assuming that a indexes from 0 to 9, what happens when we use p with an out of range index, for example:
int *p = &a[8];
blah = p[3];
My main concern is how to tell other functions that the array has a maximum size, and how to determine (inside a function) what the maximum length of its parameters is.
- WalterBright 4y ago> When `p` is a parameter in a function, the function cannot know that it can create a slice of up to 10 elements (I assume that the `p[0 .. 10]` creates an array indexed from 0 - 9). That's right, when a bounds checked array is converted to a pointer, the bounds does not go with it. Presumably, the function receiving the p has some way to determine the length (such as strlen, or via another parameter) from which the correct array can be reconstructed by doing a slice. > What if the line was: int a[..] = p[0..12] Do we still get undefined behaviour? Yes, if the 12 extends past the end of the data p points to. > Assuming that a indexes from 0 to 9, what happens when we use p with an out of range index, for example: int *p = &a[8]; blah = p[3]; You get undefined behavior. > My main concern is how to tell other functions that the array has a maximum size The same way it's done now, by strlen, passing another argument with the length, or the function is able to get the length by other means. When a bounds checked array is converted to a pointer, the bounds are not part of the pointer.
- lelanthran 4y agoThank you. I'm always pleased when I get a reply from WalterBright[1]. Some follow up questions: 1. If you could redesign the above mechanism, would you do anything differently? 2. In my Own Toy Language[2], I've toyed with the idea making all native arrays fat objects as it seems the best way to ensure that the compiler, at any point, as the ability to bounds check if necessary. All that goes out the window when you want to do FFI to some C function. Any thoughts on mitigating this? [1] Sorta like telling people when you speak to some celebrity :-) [2] Everyone designs their own perfect language; I'm no different.
- WalterBright 4y ago> If you could redesign the above mechanism, would you do anything differently? Nope. It was a home run. > All that goes out the window when you want to do FFI to some C function. And there you go! What D does, though, is bounds check the conversion of an array to a pointer, and then (in code marked @safe) not allow arithmetic on the pointer. Additionally, D introduced the `ref` parameter which eliminates nearly all use cases of needing to pass by pointer to a function.