How does D distnguish managed pointers from raw pointers?
Andrea Fontana
nospam at example.com
Thu Oct 3 14:21:37 UTC 2019
On Thursday, 3 October 2019 at 14:13:55 UTC, IGotD- wrote:
> According to the GC documentation this code snippet
>
> char* p = new char[10];
> char* q = p + 6; // ok
> q = p + 11; // error: undefined behavior
> q = p - 1; // error: undefined behavior
>
> suggests that char *p is really a "fat pointer" with size
> information.
No it's not. char* is a plain pointer.
The example is wrong, since you can't assign a new char[10] to
char*.
Probably they mean something like:
auto arr = new char[10]
char* p = arr.ptr;
...
This code actually compiles, but its behaviour is undefined, so
it is a logical error.
In D arrays are fat pointer instead:
int[10] my_array;
my_array is actually a pair ptr+length.
More information about the Digitalmars-d-learn
mailing list