I’ve got the following variable (simplified case):
std::array<std::array<float, 4>, 4> matrix;
I need to return this variable from a function in my program. I could either use std::unique_ptr or return it as a value (automatic vs dynamic memory)
Since the size of a float on my platform is 4 bytes, and there are 16 positions, size would be 16 * 4 = 64 bytes.
Dynamic memory without custom allocators etc can cause memory fragmentation in addition to being generally slow, so I got to wondering what the practical limit of passing around data as automatic memory vs dynamic memory could be? What size should I start using dynamic memory? Is the question unanswerable?
If you are worried about the size of returning a structure like this as a result of an init function then I would not (caveats to that statement (you can always measure if you are unsure)).
All modern compilers are already doing RVO and NRVO optimization. As a result even if you return by value a copy is not being made. The compiler has already worked out that the result is going to a specific destination and has elided the copy and is building it in place at the destination.
Thus the size constraint is not really an issue.
Also with C++11 the concept of move semantics have been introduced. So you don’t need to return by value but return via a move. In most std containers this just means swapping a couple of pointers (unfortunately this does not help with std::array) but in general case it works well.
So for me it would probably be a return by value. Though I may wrap that structure in a class. That way if reality bites and it does cost a lot I could change the class internally to compensate without changing the code.