-
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
sizeof() of an struct is not treated like a constant like sizeof(int). #15116
Comments
This seems to be according to the spec:
Do you think the spec should be changed? |
In this case, For example, if you were to declare: [StructLayout(LayoutKind.Explicit, Size = 4)]
private struct PageData
{
[FieldOffset(0)]
private long _unused;
} Then It can get even more complicated in other scenarios where the layout may actually depend on runtime layout (such as if you used |
See also #3208 |
@redknightlois It is because the size of struct is indeed non-constant. It may change according to CPU architecture and memory alignment. Size of explicitly sized/layout/aligned structs does be constant though. However, in current C#, there is no notation of explicitly sized/layout/aligned (unmanaged) struct. This is also a common issue when using generic |
@tannergooding That case it is a semantic bug. If you do that, you are in deep troubles way before the compiler/runtime would arbitrarily choose one or another. That should even generate a warning at the compiler level as it is pretty easy to detect. A static typed language should be expected to limit/notice those kind of errors and probably deserves an issue on its own. @svick if you put it that way, yes. Mainly, because, @gafter agree, this is a subcase on that and yet another C# for performance high in my wish list :). Having Currently this is the kind of unsafe (in the sense of brittle) code I have to write. internal unsafe struct FixedPageLocator
{
[StructLayout(LayoutKind.Explicit, Size = 4)]
private struct PageData
{
}
private fixed byte Tables[CedarRootHeader.TotalNumberOfPages * 4];
} |
If we take request/issue #15079 and its referenced sub-issues into consideration for upcoming C# versions, this should no longer be a problem 😉 |
Closing this out. We're doing all language design now at dotnet/csharplang. If you're still interested in this idea let us know and we can migrate this over to a discussion in that repo. Thanks! |
Version Used: C# 6.0
Steps to Reproduce:
Expected Behavior: At compile time
sizeof(PageData)
is a constant, for all states and purposes thesizeof
statement of should compile when you are working with an struct in the same way if you are usingsizeof(int)
.Actual Behavior: Fails with
CS0133 The expression being assigned to 'CedarPage.FixedPageLocator.Tables' must be constant
EDIT: After a second though, by-product of this change would be the ability of allowing fixed buffers for blittable types too. So this can be enabled too:
The text was updated successfully, but these errors were encountered: