Error CS353 When it doesn't make sense for this situation
A result of a stackalloc expression of type 'Span<int>' cannot be used in this context because it may be exposed outside of the containing method
however it isn't exposed out of the method, I will post the code in another message as it is a lil long
15 Replies
the rest is irrelevant but I will add it for context
The error is on the line
I might just not use spans for this situation as I am not entirely sure but it would still be good to know what was going wrong
I would check the il but it is a full on error so I can't
I did the il with sharplab and that worked and it didn't seem to expose
ah I made a rather silly mistake, the copyto has the destination as its param not the data
nope that didn't fix it
this one is really confusing me, I commented out all the return values and made it return null, the only parameter in is an integer and it is still saying that it is exposed
Declare
memoryBlocks
as scoped
scoped Span<int> memoryBlocks...
The default safe-to-escape scope is not scoped
And you're not initializing it with something that would narrow it (like assigning a stackalloc in the initializer)I can't use scoped as my language version for this can't be high enough but I could assign it, is there any way to have it without allocating it, preferably it should be very rarely allocated, but I could allocate it if I have to
also what is scoped, I haven't come across that before and I can't see it in the docs
ah found it in the c# 11 page
Do a
stackalloc int[0];
I'm pretty sure that worksyeah ok will do, why is it that it has to be allocated?
that does work thanks
.
I don't quite understand
By default, the local can escape the method
That's it
Doesn't matter whether it does. It can
You can narrow that default by assigning something that not safe-to-escape
Or, in C# 11, you can declare the local as
scoped
ah ok that makes more sense, why isn't it implicitly not safe to escape by already being defined as a valuetype in the locals
Being a valuetype has nothing to do with safe-to-escape
it should also be defined as a Span<int> there too also wouldn't it
Span<char> s = " test";
is perfectly fine to escape the methodwhat isn't safe to escape?
actually a better question would be, what does it mean for something to escape the method
Span<char> Get() => "Test"; // escapes, it's heap data, this is fine
Span<char> Get() => stackalloc char[1] { 'c' }; // escapes, oops
ahhh ok thanks that clarifies it