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.

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?

Reply via email to