Reputation: 1289
I would like a function to return a dynamic number of bytes, in the range of 1K - 60K. I cannot use dynamic memory allocation, so I can't use Vec
. I also cannot return a slice to a function-local array. Rust doesn't have gcc-like dynamic stack arrays. I don't believe I can resize an array, i.e. allocate a larger fixed array, then release some of the memory at the end. What other options do I have?
Upvotes: 1
Views: 1528
Reputation: 2810
It looks like the thing you are looking for is not possible.
I cannot use dynamic memory allocation
For me it sounds like compiler-generated code can't use dynamic memory allocation (heap) as well. Then the only place where you can save the whole result is caller's stack. The stack size have to be known at compile time.
The possible solution may be to return array with predefined size and the size of actual data. Like
fn generate_data() -> ([i32; 50000], usize)
Or you may allocate memory on caller's stack and then provide reference to data-generation function:
fn generate_data(out: &mut [i32; 50000]) -> usize
Otherwise if you are OK with heap allocation by compiler-generated code then it's not clear why you can't use dynamic allocations by yourself.
Upvotes: 1