1

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?

11
  • 2
    You can write data into a file. Commented Jan 21, 2020 at 15:19
  • 2
    cannot return a slice to a function-local array — correct, but you can take a slice and return a subslice of it: fn foo(data: &mut [u8]) -> &[u8]. Then declare the array with the maximum size and pass it in. Commented Jan 21, 2020 at 15:22
  • 1
    If you mean a dynamic-sized array, nope, that's not (yet) possible. See github.com/rust-lang/rust/issues/48055 Commented Jan 21, 2020 at 16:08
  • 2
    @Shepmaster Both the duplicates you are suggesting appear to be about arrays with sizes chosen at compile time. My understanding is that this question is about dynamically choosing the size at run time. (And I believe your suggestion to return a subslice of a slice received as parameter is the best answer.) Commented Jan 21, 2020 at 16:49
  • 1
    tinyvec should handle any size that would fit on the stack with a static-sized array. However, even with tinyvec you're stuck with overallocating and shrinking if you want an exact-sized answer. I'd still recommend using it, because otherwise you're just going to end up using unsafe to use realloc or memmove or something. The only other real solution is to arena allocate your arrays (i.e. prealloc a huge pool on the stack, fill the first index, then advance the memory pointer to the end, but then you're just doing manual memory management). Commented Jan 21, 2020 at 22:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.