www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Pointer types [Was: Re: Lints, Condate and bugs]

reply bearophile <bearophileHUGS lycos.com> writes:
Walter:

 Microsoft's Managed C++ does exactly this. While a technical success, it is a
 complete failure in regards to pleasing programmers.
 
 I'm also very familiar with using multiple pointer types, which are a necessity
 for DOS programming. I'm sick of it. It sucks. I don't want to go back to it,
 and I don't know anyone who does.
I was talking about a different kind of pointer types, something not related to the CPU/OS. Three possible annotations for pointers: - single: this pointer is either null or points to a single element of the base type (this disallows the indexing [] syntax). - bound(hi,lo): this pointer is either null or points inside an array of given lower and upper bounds. - sentinel: this pointer is useful only for comparisons and not for dereference or indexing. This annotation is usually used for pointers that point immediately after an allocated area. You may implement them with just struct templates: static assert(SinglePtr!int.sizeof == size_t.sizeof); static assert(BoundPtr!int.sizeof == (size_t.sizeof * 3)); static assert(SentinelPtr!int.sizeof == size_t.sizeof); int x; SinglePtr!int p = singlePtr(&x); SentinelPtr!int s = sentinelPtr(&(x) + 1); int[5] a; BoundPtr!int r1 = boundPtr(a); BoundPtr!int r2 = boundPtr(a, 1, 4); They are useful if you write low-level code in D, or if you want to use C libraries through D in a safer way (probably lot of C code will be used from D in the next years). I have implemented BoundPtr some time ago for D2. Other possible features for them: - head const - tail const - const - no pointer arithmetic - not null The advantage of using struct templates is that there is no need to change D2. A disadvantage of using structs is that they can't be used in C function signatures (and their syntax is more heavy): extern(C) void foo( singleptr int*, boundptr(10) int*); Bye, bearophile
Oct 31 2010
parent reply Walter Bright <newshound2 digitalmars.com> writes:
bearophile wrote:
 Three possible annotations for pointers:
 
 -  single: this pointer is either null or points to a single element of the
 base type (this disallows the indexing [] syntax).
ref T single = *p;
 -  bound(hi,lo): this pointer is either null or points inside an array of
 given lower and upper bounds.
T[] bound = p[lo..hi];
 -  sentinel: this pointer is useful only for comparisons and not for
 dereference or indexing. This annotation is usually used for pointers that
 point immediately after an allocated area.
T[] sentinel = p[0..0];
Oct 31 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Walter:

 -  single: this pointer is either null or points to a single element of the
 base type (this disallows the indexing [] syntax).
ref T single = *p;
Is this a D syntax or C++ syntax? I have never seen this used in D unless as function argument.
 -  bound(hi,lo): this pointer is either null or points inside an array of
 given lower and upper bounds.
T[] bound = p[lo..hi];
I was talking about a (fat) pointer, something you may move forward and backward, assign, deferentiate, you may perform arithmetic on it, etc. This is why I have written in my post: static assert(BoundPtr!int.sizeof == (size_t.sizeof * 3));
 -  sentinel: this pointer is useful only for comparisons and not for
 dereference or indexing. This annotation is usually used for pointers that
 point immediately after an allocated area.
T[] sentinel = p[0..0];
That's not a pointer. Bye, bearophile
Oct 31 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Walter:

 T[] sentinel = p[0..0];
You can't compare it with other pointers, because it's not a pointer. If you use sentinel.ptr you have zero protection against static disa. You can't define a C signature with that. And it uses two words, while the sentinel/SentinelPtr need just one word. So I think it does no one of the purposes a sentinel pointer was meant to do. Bye, bearophile
Oct 31 2010