C# StructLayout/FieldOffset 和数组索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/839903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:39:07  来源:igfitidea点击:

C# StructLayout/FieldOffset and indexing in arrays

c#arraysindexingalignmentfield

提问by Burre

I'm having a bit of a problem using FieldOffset correctly with arrays. The code below is an example where it doesn't work correctly for me:

我在数组中正确使用 FieldOffset 时遇到了一些问题。下面的代码是一个例子,它对我来说不能正常工作:

[StructLayout(LayoutKind.Explicit)]
public struct IndexStruct {
    [FieldOffset(0)]
    public byte[] data;

    [FieldOffset(0)]
    public short[] idx16;

    [FieldOffset(0)]
    public int[] idx32;
}

If I for example sets the array named "data" to a serialized byte array and then try to retrieve data as shorts using the "idx16" field the indexing is still aligned as a byte[]. Meaning that idx161fetches the second byte in data, not the second 16bit word (byte 2 and 3). If I do the inverse I index shorts instead of bytes, meaning that the offset alignment is inherited from the source data. My question, is there a way to work around this? I know that I can compensate the index value by multiplying with the size of the element, but is there another way?

例如,如果我将名为“data”的数组设置为一个序列化的字节数组,然后尝试使用“idx16”字段检索作为 short 的数据,则索引仍然与 byte[] 对齐。这意味着 idx16 1获取数据中的第二个字节,而不是第二个 16 位字(字节 2 和 3)。如果我做反向 I 索引 shorts 而不是字节,这意味着偏移对齐是从源数据继承的。我的问题,有没有办法解决这个问题?我知道我可以通过乘以元素的大小来补偿索引值,但是还有其他方法吗?

Hereis an answer I found here on StackOverflow, but when trying that code it turned out that it wasn't working properly. Tried it out using a Unit test in VS with the following code without any success:

是我在 StackOverflow 上找到的答案,但是在尝试该代码时发现它无法正常工作。在 VS 中使用单元测试使用以下代码进行了尝试,但没有成功:

[TestMethod()]
public void SumTest() {
    float[] fArr = {2.0f, 0.5f, 0.0f, 1.0f};
    MemoryStream ms = new MemoryStream();
    for (int i = 0; i < fArr.Length; i++) {
        ms.Write(BitConverter.GetBytes(fArr[i]), 0, sizeof(float));
    }
    byte[] buff = ms.ToArray();
    double expected = 3.5f;
    double actual = Sum(buff);
    Assert.AreEqual(expected, actual);
}

Many thanks in advance!

提前谢谢了!

采纳答案by Marc Gravell

The problem is (from what I can see) that you've unioned the referencesof the arrays - so whichever array gets set last will win. Once there is an array, it is using the indexer (not byte offset) - so the size doesn't matter.

问题是(从我所见)你已经联合了数组的引用- 所以最后设置的数组将获胜。一旦有一个数组,它就会使用索引器(而不是字节偏移量)——所以大小无关紧要。

The way to do this "properly" (or improperly, as the case may be) would probably be with unsafe code - taking the pointer to the array - something like:

“正确”(或不正确,视情况而定)执行此操作的方法可能是使用不安全的代码 - 将指针指向数组 - 类似于:

    IndexStruct s = new IndexStruct();
    s.data = new byte[] { 1, 0, 0, 0, 1, 1 };

    unsafe
    {
        fixed (short* data = s.idx16)
        {
            Console.WriteLine(data[0]); // should be 1 (little-endian)
            Console.WriteLine(data[1]); // should be 0
            Console.WriteLine(data[2]); // should be 257
        }
    }

Of course, I'm not sure I recommend it - but that seems to achieve what you want?

当然,我不确定我是否推荐它 - 但这似乎实现了你想要的?

I also wonder whether you can drop the structcompletely and just use unsafe access to a byte[]directly:

我也想知道你是否可以struct完全放弃,直接使用不安全的访问byte[]

    byte[] raw = new byte[] { 1, 0, 0, 0, 1, 1 };
    unsafe
    {
        fixed (byte* addr = raw)
        {
            short* s = (short*)addr;
            Console.WriteLine(s[0]); // should be 1
            Console.WriteLine(s[1]); // should be 0
            Console.WriteLine(s[2]); // should be 257
        }
    }

回答by datacop

Your FieldOffset defines where each of your data elements are inside the struct..

您的 FieldOffset 定义了每个数据元素在结构中的位置。

By setting them all to 0, you're telling the compiler that they all at position 0.

通过将它们全部设置为 0,您告诉编译器它们都位于位置 0。

The second thing I see is you're creating an array of bytes, shorts, and ints.

我看到的第二件事是您正在创建一个字节、shorts 和 int 数组。

see: MSDN StructLayoutAttribute

请参阅:MSDN StructLayoutAttribute

[StructLayout(LayoutKind.Explicit)]
public struct IndexStruct {
        [FieldOffset(0)]
        public byte[16] data;

        [FieldOffset(16)]
        public short idx16;

        [FieldOffset(18)]
        public int idx32;
}