|
|
|
|
|
by noob_eng
1158 days ago
|
|
> It should be read not as it's written, but rather as `return the value of a` So values that are returned need not be primitive values like basic ints, floats, strings, etc? They can be complicated structure values also? Got it, then! |
|
But in e.g. C, structs and arrays can also be allocated right on stack, which may be, and often is, preferable performance-wise. So their variables are their values, without referencing, aka “automatic storage”. Like in, 700 byte-sized struct right in stack memory. You can return such struct in C, it will be copied or “moved” as described elsewhere itt. But you cannot return an automatic array, because it decays to a pointer to stack memory that gets reclaimed right after return, and accessing through such pointer is a programming error. To deal with this limitation you either return a pointer to a heap—allocated array, or accept an array as an argument instead, offloading the storage class responsibility to the caller. Same thing people do with structs really, except for very small ones, to avoid copying and related issues.