-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
can stackalloc be allowed in ref struct constructors? #35658
Comments
Presumably you're trying to say that since a |
A constructor is just a regular method. If you invoke it and it returns, the stack frame of that method goes away (along with any |
Thanks for clarifying. I think I understand. The constructor is a regular method and the parameters need to be cleaned from the stack, and with that the stackalloc'ed memory is gone. Don't want to be difficult but we could ask ourselves if (in the case of a ref struct constructor) it has to be a regular method or if there is another way. |
"variable size structures" sound like trouble for the runtime, whether they are stack-allocated or not. I don't know if that's supported.
It would be good to identify some motivating examples and start a discussion on |
It's powerful until someone passes large The only way to do this today is to allocate stack memory in the caller but that's going to be cumbersome, at least because you can no longer rely on the C++, which usually can do a lot of crazy things, is too rather limited in this regard - you can have a variable sized struct but the language doesn't offer much support for that, practically none. Its only advantage would be "placement new" that allows you to construct an object in previously allocated memory. Not sure if that's something that would make sense in C#, most likely not. Ultimately you might be better served by something like: public ref struct Matrix<T> where T: unmanaged
{
private readonly int w;
private readonly int h;
private readonly Span<T> span;
public Matrix(int w, int h, Span<T> span)
{
this.w = w;
this.h = h;
if (span.Length != w * h) throw ArgumentException(); which allows the caller to decide how the memory is allocated. |
Version Used:
c# 7.3
Steps to Reproduce:
results in error
CS8353: A result of a stackalloc expression of type 'Span<T>' cannot be used in this context because it may be exposed outside of the containing method
I think I understand the error message and why it is necessary to prevent it.
But in this specific case the this.span is a field on a ref struct itself, so the stack will be maintained and the matrix itself cannot "be exposed outside the context".
The text was updated successfully, but these errors were encountered: