-
Notifications
You must be signed in to change notification settings - Fork 89
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
structs with VariableLengthInlineArray and SizeOf(0) #1179
Comments
That's an interesting problem. Am I correct in supposing it's important for you to get the size of the struct that reflects a 0 length trailing buffer specifically, such that passing a size that implies a length of 1 causes a malfunction? Or is this just to 'feel good' about the size matching what C would say? It's potentially problematic to change the behavior to match what you say, because to C#, the If this really is important, we may need to introduce another function alongside |
My use case could definitely be rewritten to use the current way CsWin32 generates the struct. But before CsWin32 supported the USB structures I already had my own definition as here: I use it "C-style" to custom marshal the data that comes after it, which is not really byte data, but instead another structure. And with "C-style" I mean: use offset into the byte representation to get to the start of that following data struct. And that offset in C is just sizeof(USB_DESCRIPTOR_REQUEST). But not in C#, where there currently is no way to get the that original size (except subtracting the 1 byte manually). Here is how I use it (note: this code predates the CsWin32 support for these structures. I am slowly trying to get rid of all my manual WinSDK "defines" and move to CsWin32 for everything. This thing just struck me. And like I said, I can easily rewrite it to work with CsWin32. I was just wondering why it is not possible to get the original C size for "sizeof(USB_DESCRIPTOR_REQUEST)". On a side note: what if, instead of adding the empty struct with [] operator (which
Instead of addressing the flexible array like And on that last note: why is the |
I think you have some good ideas here. I haven't the time this week probably, but I'll review more closely and see what we can adopt. |
Thanks. But that's what they realy are ... ideas ... not proposals. Like I said, I could use the generated types as they are by just rewriting a little bit of code. But if you are interested in the "flexible array without a 1-byte empty struct" idea. There are some corner cases to consider. Such as alignment, for example a C struct:
In C this will have alignment 4, size 4, and a byte of padding between c and array. But if you simply make a C# struct with the first 3 members and a kludge method like I suggested, then of course you need something like pack(4). In that case a simple dummy padding member is not enough, as that would make the size 4, but still have alignment 1. |
C structures with a flexible array member at the end (array of [0] elements) get translated to a
VariableLengthInLineArray
. As an example, I take USB_DESCRIPTOR_REQUEST, which in C is:I know about the discussion that in C# (using CsWin32) the
VariableLengthInLineArray
always has 1 member (as ifUCHAR Data[1]
instead ofUCHAR Data[0]
), and therefore the sizeof(C# struct) does not match the sizeof(C struct). And that's OK, since we now have a convenience member functionSizeOf
for such structures that takes a count parameter for the real number of elements in the array.However, the function is defined as:
This does not return the correct size for 0; SizeOf(0) is still off by 1 array element. In fact SizeOf(0) == SizeOf(1) in the current implementation. Note that for the example USB_DESCRIPTOR_REQUEST, 0 is actually a valid size for the array (as in: the data is optional).
I don't know if this is deliberate, or actually a bug.
Could
SizeOf(0)
be changed so it returns sizeof(C# struct) - sizeof(the 1 element in VariableLengthInLineArray) == sizeof(C struct)? Something like:The text was updated successfully, but these errors were encountered: