Slicing to convert pointers to bound arrays

simendsjo simen.endsjo at pandavre.com
Sat Aug 7 05:45:52 PDT 2010


I understand this is an unsafe operation, I just have a quick question.


Here, b points outside a's memory, so I get garbage.
	{
		int[] a = [3,3,3];
		auto p = a.ptr;
		auto b = p[0..3];
		assert(b == [3,3,3]);
		b = p[0..4]; // b == [3,3,3,?]
	}


When I do the same with a static array, I get 0. But this is just 
actually garbage, right? It might contain other data, and not always 0?

	{
		int[3] a = [3,3,3];
		auto p = a.ptr;
		auto b = p[0..3];
		assert(b == [3,3,3]);
		b = p[0..4];
		assert(b == [3,3,3,0]);
	}


More information about the Digitalmars-d-learn mailing list