stackalloc return #923
Replies: 5 comments 1 reply
-
Normal arrays can only be allocated and stored on the heap. If |
Beta Was this translation helpful? Give feedback.
-
Yes, that's what I thought too. if stack is popped then array will be lost. that's why I mentioned it maybe copied before its returned? for example a pointer would be passed and on top of stack, before returning array will be copied there, so once you pop you have the array. I guess this is possible with c#7 which introduces ref locals/returns. |
Beta Was this translation helpful? Give feedback.
-
The only way that would work (in any language) is if the caller were to preallocate a block of memory and pass a pointer to the function. You can do that today in C#. With |
Beta Was this translation helpful? Give feedback.
-
I see, so that requires size of array to be known before calling method. then I guess it should be like this.
with a little twist
|
Beta Was this translation helpful? Give feedback.
-
Yes, but then the function needs to accept that pointer so that it can write to it. At that point you might as well allocate and pass the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
is stackalloc return possible? it would be used to prevent creating array on heap, array would be created on stack and its pointer is returned, (or maybe copied in stack once its popped?)
Beta Was this translation helpful? Give feedback.
All reactions