Herb Sutter on zero cost exceptions for C++

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Sep 30 20:49:24 UTC 2019


On Fri, Sep 27, 2019 at 07:13:36PM +0200, Timon Gehr via Digitalmars-d wrote:
[...]
> > A "non-owning" pointer cannot be passed to free() (or equivalent).
> 
> Issue is you can have a non-owning pointer pointing to a data
> structure that contains owning pointers.
[...]
> [...] E.g., you can have two non-owning pointers to a Array!int and
> then one of them reassigns the array, while the other borrowed a
> pointer to one of its elements.

Could you give a specific example with pseudocode?  I'm having trouble
understanding exactly what goes wrong here.  I got as far as:

	struct S {
		OwnerPtr!int value;
	}
	OwnerPtr!S s = allocate!S();
	S* s1 = s;	// borrow
	S* s2 = s;	// borrow

	int* elem = s1.value; // borrow from contained owning ptr
	s2.value = new OwnerPtr!int(123); // invalidates elem

Doesn't this just mean that non-ownership should be transitive? I.e., if
you have a non-owning pointer to some data structure, then all
OwnerPtr's in the data structure effectively become non-owned from your
POV, so reassigning is illegal.


> Rust enforces no mutable aliasing exactly because reassigning
> something may cause the old value to be deallocated.

Doesn't that just mean that the borrowed pointer had a lifetime that
exceeded the lifetime of the owning pointer?  Therefore, it was
(retroactively) illegal to have borrowed that pointer in the first
place, since you couldn't guarantee that the owning pointer wouldn't
become invalidated.  Transitive non-ownership seems to be one way of
dealing with this (though I'm not sure whether it's sufficient, or even
consistent -- have to think about this more).


T

-- 
I am not young enough to know everything. -- Oscar Wilde


More information about the Digitalmars-d mailing list