Is it "OK" to only keep the ptr part of alloc()?

Sometimes I don´t really need the built-in len of []T and it would be enough to only store [*]T.

This seems to work with x = (try allocator.alloc(T, n)).ptr; and allocator.free(x[0..n]);. Is there a particular reason not to do this, apart from potentially messing up, and using the wrong n when freeing?

The reason I want to do it in the first place, is to store n in a u32 instead of a usize. Then I can have another u32, to keep track of something else for the same memory budget.

Your example should work fine.

One thing to be aware of is if you detach the length from the pointer, you’ll probably lose automatic out-of-bounds checking, not the end of the world but something to consider.

1 Like