3 ms·
I'm a little bit confused on what this means. By array I guess you mean a dynamically sized, but not dynamically-resizable pointer and size/length pair? So you
by chlorion 3y ago
I'm a little bit confused on what this means.
By array I guess you mean a dynamically sized, but not dynamically-resizable pointer and size/length pair? So you aren't storing the "capacity" like Rust's Vec would be doing.
You can do the same thing in Rust with Box<[T]> if I am understanding you correctly. Box<[T]> in this specific case is a fat pointer, which is a pointer to the underlying allocation and a length.
One of the issues with Box though, at least as of the last time I looked at it, is that the only way you could create a Box<[T]> without unsafe or nightly was to create a Vec, and then call into_boxed_slice on it. The conversion from Vec to Box actually causes a new allocation to be created if the size and capacity fields in the Vec are not equal. In C it would be possible to reuse the Vec's buffer, but dealloc (and all other alloc related functions it seems) in Rust requires passing in information about the layout the of the underlying allocation, and size is part of the layout!
In C++ I guess you would want to use std::unique_ptr<T[]>, and manually store the length, which is not great but still works. I'm not sure if unique_ptr is guaranteed to be the size of a pointer or not, so this may or may not work.
Regular statically-sized arrays are the same size in each language ofc. In Rust and C++ statically-sized arrays do have one benefit though, you get bounds checking on them "for free". Since the length is baked into the type, the accessor methods know the bounds at compile time, so no runtime length information is required to do runtime bounds checking! You can do this in C by hand for each array you define but that is impractical, you could probably use a macro to do this though.
If I did understand what you meant, I think this brings up an interesting topic. Having a first-class dynamically-sized non-resizable array type can be pretty useful! Rust already can do this awkwardly with Box, but C++ currently doesn't have a way to do this without manually storing the length and doing manual bounds checking that I know of. It would not be terribly difficult to implement this and there probably are libraries that exist for it, but I still thing it's interesting.
# comment for Vec::into_boxed_slice
https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1075 https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1075
# rust std::alloc::dealloc requires std::alloc::Layout
https://doc.rust-lang.org/std/alloc/fn.dealloc.html https://doc.rust-lang.org/std/alloc/fn.dealloc.html
- gavinhoward 3y agoI meant static arrays. I have to use a pointer+length pair because many system functions in C require a pointer, but I want bounds for bounds checking. Yes, I do my own arrays in C. I also have dynamic arrays, but those are not 16 bytes, and I treat them differently.
- chlorion 3y agoDo you have your own array library that you use in your projects? If so that's pretty cool and it would be interesting to see. Edit: i looked at your profiles information and have discovered lots of fun stuff
- gavinhoward 3y agoThank you! As you have probably discovered, my array stuff is just part of a monorepo. But yes. :) Edit: The code at the Yzena one is not up to date because I've had to keep commits local. But I have 1200+ commits since, and I plan to make them public in April.