I've read a lot on Rust lately but am still only beginning to oxidize. My brain retains most of its C/C++ reflexes so pardon me if this question is not relevant because of how things are done in Rust.
Is it generally possible (desirable?) to allocate a block of an arbitrary size on the heap and then map a data structure over it with bindings taking ownership of smaller chunks of memory?
Using C99, one may write this:
typedef unsigned char BYTE;
typedef struct {
size_t size;
BYTE payload[];
} flex;
// ...
flex * flex_new(size_t _size) {
flex *f = malloc(sizeof(flex) + _size * sizeof(BYTE));
f->size = _size;
return f;
}
Flexible length arrays may prove useful e.g. when implementing memory pools with chunks of variable size or when allocating memory on the heap dedicated to a thread. The data structure which required size is only known at runtime is allocated in an 'atomic' fashion and its members packed contiguously.
I am wondering whether a similar Rust implementation is possible (without unsafe
constructs) and if it is, what it looks like. Maybe the compiler may infer some information using lifetime specifiers in the struct
definition?
unsafe
. It doesn't help that many libraries that you'd expect are doing this actually found different ways:Vec
is a(ptr, length, capacity)
triple rather than a pointer to(length, capacity, items)
, andArena
piggybacks onVec
s that are never resized. – user395760Arena
and will look into it at once, thanks! I should have stated that I don't care ifunsafe
is used as long as a safe interface is provided instd
or as a candidate for it. – dummydev