Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] how to return stack allocated bytes without putting it in the heap? #62127

Closed
Xyncgas opened this issue Nov 29, 2021 · 5 comments
Closed
Labels
untriaged New issue has not been triaged by the area owner

Comments

@Xyncgas
Copy link

Xyncgas commented Nov 29, 2021

I am pretty sure it's just 256 bytes maximum while the usual case being 7 bytes.
I would like to return them in the manner like I would return a int.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Nov 29, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@gfoidl
Copy link
Member

gfoidl commented Nov 29, 2021

The stack is "method scoped", so it's gone when the method (i.e. the scope) is left. That's how stack works.
So you can't return stack allocated bytes to the caller.

like I would return a int.

int is a value-type, and it's copied by value. You could create a custom struct*, which as being a value-type, can be returned by copy too.

* could use fixed sized buffers or by specifing the size of the struct to be 256

@Joe4evr
Copy link
Contributor

Joe4evr commented Nov 29, 2021

Yes, the short answer is "you can't".

However, there is talk of adding a method that is specifically designed for such a purpose: #52065

These APIs would be intrinsic to the JIT [...] specially inlined into the function so the localloc scope is that of the calling method

@svick
Copy link
Contributor

svick commented Nov 29, 2021

@Joe4evr The method proposed there violates the "you can't return stack-allocated bytes" principle, but I don't think it will let you violate it yourself, so it wouldn't be useful here.

@Xyncgas
Copy link
Author

Xyncgas commented Nov 29, 2021

the api looks nice, right n I am just passing in span/, that's already allocated in stack to the function I am calling, it would be nice if I can just get it from the function I am calling though and I get the method to return int or a struct or something but it's not very convenient when the size of the bytes I am returning is changing but I have a way around it too by using a fixed size buffer and another byte that tells me how many bytes actually returned so basically I am getting a frame back from the function, since I am beating to bush for around 1~16 bytes I should be fine

@Xyncgas Xyncgas closed this as completed Nov 29, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Dec 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

4 participants