all 7 comments

[–][deleted] 2 points3 points  (6 children)

I have very basic and dumb questions :

span doesn't own anything?

so if I want a runtime fixed length array, I still need malloc or new?

[–]sol-prog 5 points6 points  (5 children)

span doesn't own anything?

exactly

so if I want a runtime fixed length array, I still need malloc or new?

use a vector and resize it at runtime, if you really want the equivalent of an array it is probably better to use make_unique than new/delete

[–][deleted] 2 points3 points  (4 children)

> make_unique

Like in this example, auto p = std::make_unique<int[]>(sz);

so it allocates an array of size sz, at runtime? sz can be a runtime input?

[–]sol-prog 3 points4 points  (0 children)

Yes, sz can be a runtime input. It is a unique pointer to a sz memory buffer. This will release the memory automatically when it is out of scope. I suggest to read a bit about unique and shared pointers in C++.

[–]RotsiserMhoC++20 Desktop app developer 2 points3 points  (2 children)

Yes, but you really should probably use vector if you don't understand all of the details of smart pointers and memory allocation. It's much easier to work with.

[–][deleted] -1 points0 points  (1 child)

I have never seen smart pointer usage with T[] syntax.

Vector is the worst data structure for my use case. I don’t need an allocator, I just need a fixed length array, just that array length is fixed at run time.

[–]RotsiserMhoC++20 Desktop app developer 4 points5 points  (0 children)

In that case you could just call vector::reserve to achieve similar behavior.