Bug 12444 – std.array uninitializedArray & minimallyInitializedArray missing APPENDABLE attribute / capacity info

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-23T03:48:59Z
Last change time
2018-06-21T15:43:02Z
Assigned to
No Owner
Creator
safety0ff.bugz

Comments

Comment #0 by safety0ff.bugz — 2014-03-23T03:48:59Z
import std.array; import std.stdio; import core.memory; void main() { double[] a = uninitializedArray!(double[])(100); a = a[0 .. 1]; assert(a.capacity == 0); a.assumeSafeAppend(); assert(a.capacity != 0); // Error double[] b = minimallyInitializedArray!(double[])(100); b = b[0 .. 1]; assert(b.capacity == 0); b.assumeSafeAppend(); assert(b.capacity != 0); // Error double[] c = new double[100]; c = c[0 .. 1]; assert(c.capacity == 0); c.assumeSafeAppend(); assert(c.capacity != 0); // OK! auto dptr = cast(double*)GC.malloc(100 * double.sizeof, GC.BlkAttr.NO_SCAN | GC.BlkAttr.APPENDABLE); double[] d = dptr[0 .. 100]; d = d[0 .. 1]; assert(d.capacity == 0); d.assumeSafeAppend(); assert(d.capacity != 0); // OK! }
Comment #1 by safety0ff.bugz — 2014-03-23T21:30:08Z
Comment #2 by monarchdodra — 2014-03-24T01:58:57Z
Just to be clear, this issue is NOT invalid. It's just that your proposed solution doesn't work. An implementation that could work is: T[] uninitializedArray(T)(size_t n) { T[] buff; //Declare buff. //Use the GC to do an Appendable allocation buff.reserve(n); //Slice out of bounds.... buff = buff.ptr[0 .. n]; //And tell the GC what the actual new bounds are. buff = assumeSafeAppend(buff); return buff; } This could not work up until now, because `assumeSafeAppend` was not nothrow. It's not nothrow anymore, but it's not yet pure either, so it still isn't useable.
Comment #3 by schveiguy — 2018-06-21T15:43:02Z
Fixed as a side-effect of issue 18995. std.array.array also had the problem that it wasn't calling the destructors. The solution is to make uninitializedArray use the druntime equivalent function (which sets up all the right bits and the finalizers, including APPENDABLE). *** This issue has been marked as a duplicate of issue 18995 ***