Am Fri, 13 Dec 2013 19:47:34 +0100 schrieb "FreeSlave" <freeslav...@gmail.com>:
> Suppose some class contains slice (dynamic array) as data member. > Constructor takes integer value that defines the size of data > should be allocated. After object was constructed size of slice > never changes by design. > > Dynamic arrays are allocated with extra space that we can know > from 'capacity' property. But how can I allocate exactly as much > elements as I want? Because in case described above we don't need > extra space. There was a recent discussion about the capacity of a dynamic array that is allocated with a known size (e.g. new int[](25)). I think the extra space comes from the way the GC allocates blocks of memory in preset sizes. Actually a malloc implementation could also have some hidden overallocation. > I know it's possible to use malloc in constructor and free in > destructor. But what if I want to expose inner slice by shallow > copy (i.e. by copying pointer and size, but not underlined data) > then use it just like usual dynamic array whose lifetime does not > depend on lifetime of an object it belongs and is managed by GC? Have you tried this?: import core.memory; int[] data = (cast(int*)GC.malloc(size * int.sizeof, GC.BlkAttr.NO_SCAN))[0 .. size]; -- Marco