首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将uint*转换为uint[]

无法将uint*转换为uint[]
EN

Stack Overflow用户
提问于 2011-09-27 03:49:40
回答 3查看 1.5K关注 0票数 7

我有一段没有编译的代码:

代码语言:javascript
复制
public struct MyStruct
{
    private fixed uint myUints[32];
    public uint[] MyUints
    {
        get
        {
            return this.myUints;
        }
        set
        {
            this.myUints = value;
        }
    }
}

现在,我知道为什么代码不能编译,但我显然已经到了疲惫不堪的地步,需要一些帮助才能将我推向正确的方向。我已经有一段时间没有使用不安全的代码了,但是我很确定我需要做一个Array.Copy (或者Buffer.BlockCopy?)并返回数组的副本,但是这些副本不接受我需要的参数。我忘了什么?

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-27 04:01:04

在使用fixed缓冲区时,必须在fixed上下文中工作:

代码语言:javascript
复制
public unsafe struct MyStruct {
    private fixed uint myUints[32];
    public uint[] MyUints {
        get {
            uint[] array = new uint[32];
            fixed (uint* p = myUints) {
                for (int i = 0; i < 32; i++) {
                    array[i] = p[i];
                }
            }
            return array;
        }
        set {
            fixed (uint* p = myUints) {
                for (int i = 0; i < 32; i++) {
                    p[i] = value[i];
                }
            }
        }
    }
}
票数 5
EN

Stack Overflow用户

发布于 2011-09-27 04:01:47

也许有一个更简单的解决方案,但这是可行的:

代码语言:javascript
复制
public unsafe struct MyStruct
{
    private fixed uint myUints[32];
    public uint[] MyUints
    {
        get
        {
            fixed (uint* ptr = myUints)
            {
                uint[] result = new uint[32];
                for (int i = 0; i < result.Length; i++)
                    result[i] = ptr[i];
                return result;
            }
        }
        set
        {
            // TODO: make sure value's length is 32
            fixed (uint* ptr = myUints)
            {
                for (int i = 0; i < value.Length; i++)
                    ptr[i] = value[i];
            }
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2011-09-27 05:08:55

这只适用于intfloatbytechardouble,但您可以使用Marshal.Copy()将数据从固定数组移动到托管数组。

示例:

代码语言:javascript
复制
class Program
{
    static void Main(string[] args)
    {
        MyStruct s = new MyStruct();

        s.MyUints = new int[] { 
            1, 2, 3, 4, 5, 6, 7, 8, 
            9, 10, 11, 12, 13, 14, 15, 16, 
            1, 2, 3, 4, 5, 6, 7, 8, 
            9, 10, 11, 12, 13, 14, 15, 16 };

        int[] chk = s.MyUints;
        // chk containts the proper values
    }
}

public unsafe struct MyStruct
{
    public const int Count = 32; //array size const
    fixed int myUints[Count];

    public int[] MyUints
    {
        get
        {
            int[] res = new int[Count];
            fixed (int* ptr = myUints)
            {
                Marshal.Copy(new IntPtr(ptr), res, 0, Count);
            }
            return res;
        }
        set
        {
            fixed (int* ptr = myUints)
            {
                Marshal.Copy(value, 0, new IntPtr(ptr), Count);
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7564015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档