Allow sizeof on variables #4429
Replies: 3 comments 7 replies
-
In the first example, sizeof(members) is not allowed because it is implementation defined. The array does not store only the integers itself, it stores in addition a virtual method table, a length and maybe something more. But for stackallow, the result is no array. It is a pointer or a Span in new code. You can get the size now via (Span is a struct which combines a pointer with a length). |
Beta Was this translation helpful? Give feedback.
-
With Span of T, you don't need a second variable to store the stack allocated array length. So you can write code similar to heap allocated arrays:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As described in the title, currently something like this is not possible:
It's not a big deal with simple arrays because you can solve it like this:
but it gets a little bit more complicated with stackalloc because you have to store the size somewhere or have magic numbers floating randomly in your code
It would be very helpful, especially when working with unmanaged code which often expects size.
Beta Was this translation helpful? Give feedback.
All reactions