How does D distnguish managed pointers from raw pointers?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Oct 4 18:43:34 UTC 2019


On Fri, Oct 04, 2019 at 06:34:40PM +0000, Dennis via Digitalmars-d-learn wrote:
> On Friday, 4 October 2019 at 18:30:17 UTC, IGotD- wrote:
> > What if you pass a static array to a function that expects a dynamic
> > array. Will D automatically create a dynamic array from the static
> > array?
> 
> No, you have to append [] to create a slice from the static array.

Actually, it *does* automatically convert the static array to a slice.
Which is actually a bug, because you get problems like this:

	int[] func() {
		int[5] data = [ 1, 2, 3, 4, 5 ];
		return data; // implicit conversion to int[]
	}
	void main() {
		auto data = func();
		// Oops: data now references out-of-scope elements on the stack.
		// Expect garbage values and stack corruption exploits.
	}

See:
	https://issues.dlang.org/show_bug.cgi?id=15932


T

-- 
"How are you doing?" "Doing what?"


More information about the Digitalmars-d-learn mailing list