• C#使用指针复制字节数组


    下面的示例使用指针将字节从一个数组复制到另一个数组。

    此示例使用 unsafe 关键字,它使您能够在 Copy 方法中使用指针。 fixed 语句用于声明指向源数组和目标数组的指针。 这将锁定源数组和目标数组在内存中的位置,使其不会因为垃圾回收操作而移动。

    这些数组的内存块将在 fixed 块结束时取消锁定。 因为此示例中的 Copy 方法使用了 unsafe 关键字,所以必须使用 /unsafe 编译器选项编译此方法。

    若要在 Visual Studio 中设置选项,请右击项目名称,然后单击“属性”。 “生成”选项卡上,选择“允许不安全代码”。

    class TestCopy
    {
        // The unsafe keyword allows pointers to be used in the following method.
    
        static unsafe void Copy(byte[] source, int sourceOffset, byte[] target,
            int targetOffset, int count)
        {
            // If either array is not instantiated, you cannot complete the copy.
            if ((source == null) || (target == null))
            {
                throw new System.ArgumentException();
            }
    
            // If either offset, or the number of bytes to copy, is negative, you
            // cannot complete the copy.
            if ((sourceOffset < 0) || (targetOffset < 0) || (count < 0))
            {
                throw new System.ArgumentException();
            }
    
            // If the number of bytes from the offset to the end of the array is 
            // less than the number of bytes you want to copy, you cannot complete
            // the copy. 
            if ((source.Length - sourceOffset < count) ||
                (target.Length - targetOffset < count))
            {
                throw new System.ArgumentException();
            }
    
            // The following fixed statement pins the location of the source and
            // target objects in memory so that they will not be moved by garbage
            // collection.
            fixed (byte* pSource = source, pTarget = target)
            {
                // Set the starting points in source and target for the copying.
                byte* ps = pSource + sourceOffset;
                byte* pt = pTarget + targetOffset;
    
                // Copy the specified number of bytes from source to target.
                for (int i = 0; i < count; i++)
                {
                    *pt = *ps;
                    pt++;
                    ps++;
                }
            }
        }
    
        static void Main()
        {
            // Create two arrays of the same length.
            int length = 100;
            byte[] byteArray1 = new byte[length];
            byte[] byteArray2 = new byte[length];
    
            // Fill byteArray1 with 0 - 99.
            for (int i = 0; i < length; ++i)
            {
                byteArray1[i] = (byte)i;
            }
    
            // Display the first 10 elements in byteArray1.
            System.Console.WriteLine("The first 10 elements of the original are:");
            for (int i = 0; i < 10; ++i)
            {
                System.Console.Write(byteArray1[i] + " ");
            }
            System.Console.WriteLine("
    ");
    
            // Copy the contents of byteArray1 to byteArray2.
            Copy(byteArray1, 0, byteArray2, 0, length);
    
            // Display the first 10 elements in the copy, byteArray2.
            System.Console.WriteLine("The first 10 elements of the copy are:");
            for (int i = 0; i < 10; ++i)
            {
                System.Console.Write(byteArray2[i] + " ");
            }
            System.Console.WriteLine("
    ");
    
            // Copy the contents of the last 10 elements of byteArray1 to the 
            // beginning of byteArray2.
            // The offset specifies where the copying begins in the source array.
            int offset = length - 10;
            Copy(byteArray1, offset, byteArray2, 0, length - offset);
    
            // Display the first 10 elements in the copy, byteArray2.
            System.Console.WriteLine("The first 10 elements of the copy are:");
            for (int i = 0; i < 10; ++i)
            {
                System.Console.Write(byteArray2[i] + " ");
            }
            System.Console.WriteLine("
    ");
        }
    }
    /* Output:
        The first 10 elements of the original are:
        0 1 2 3 4 5 6 7 8 9
    
        The first 10 elements of the copy are:
        0 1 2 3 4 5 6 7 8 9
    
        The first 10 elements of the copy are:
        90 91 92 93 94 95 96 97 98 99
    */
  • 相关阅读:
    .htaccess注释
    Ubuntu开机自启动jar包和Nginx
    Rook部署和管理Ceph集群
    Python 打包 Nuitka
    Python 反射 备查
    Python 屏幕坐标点取色
    Python pynput 监听事件
    【线性代数】基本概念
    C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
    Asp.Net Core Swagger 接口分组(支持接口一对多暴露)
  • 原文地址:https://www.cnblogs.com/2Yous/p/4888549.html
Copyright © 2020-2023  润新知