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

此示例使用 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 < ) || (targetOffset < ) || (count < ))
{
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 = ; i < count; i++)
{
*pt = *ps;
pt++;
ps++;
}
}
} static void Main()
{
// Create two arrays of the same length.
int length = ;
byte[] byteArray1 = new byte[length];
byte[] byteArray2 = new byte[length]; // Fill byteArray1 with 0 - 99.
for (int i = ; 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 = ; i < ; ++i)
{
System.Console.Write(byteArray1[i] + " ");
}
System.Console.WriteLine("\n"); // Copy the contents of byteArray1 to byteArray2.
Copy(byteArray1, , byteArray2, , length); // Display the first 10 elements in the copy, byteArray2.
System.Console.WriteLine("The first 10 elements of the copy are:");
for (int i = ; i < ; ++i)
{
System.Console.Write(byteArray2[i] + " ");
}
System.Console.WriteLine("\n"); // 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 - ;
Copy(byteArray1, offset, byteArray2, , 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 = ; i < ; ++i)
{
System.Console.Write(byteArray2[i] + " ");
}
System.Console.WriteLine("\n");
}
}
/* 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
*/

C#使用指针复制字节数组的更多相关文章

  1. 比较C#中几种常见的复制字节数组方法的效率

    在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadBytes,Buffe ...

  2. 比较C#中几种常见的复制字节数组方法的效率[转]

    [原文链接] 在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadByte ...

  3. C#指针与字节数组的操作

    private static byte[] ReadBytesFromPtr(IntPtr intPtr, int bufferLength) { var result = new byte[buff ...

  4. C#使用内存和指针方式将字节数组转换为Bitmap

    /// <summary> /// 指针方式转 /// </summary> /// <param name="Width">图像的宽</ ...

  5. C和C指针小记(十三)-数组

    1.1 一维数组 一维数组的声明: int a[10]; 这里a就是一个数组. 数组a的类型就是一个指向整型的常量指针. 但是数组和指针是**不相同**的. **数组具有特定数量的元素,而指针只是一个 ...

  6. 字节数组与String类型的转换

    还是本着上篇文章的原则,只不过在Delphi中string有点特殊! 先了解一下Delphi中的string 1. string = AnsiString = 长字符串,理论上长度不受限制,但其实受限 ...

  7. C和指针 第八章 数组

    8.1 数组名和指针 int a; int b[10]; a称为一个标量,表示一个单一的值,变量的类型是整数. b是数组,b[1]的类型是整数,b是一个指针常量,表示数组第一个元素的地址.b的类型取决 ...

  8. (IEEE-754) 字节数组与浮点数之间的互相转换(MODBUS float类型)

    在做上位机开发过程中,经常会碰到字节数组与浮点数,整数等数据之间的转换,有时为了验证数据是否解析正确,得借助于IEEE浮点数工具,本文把基于c#实现的浮点数与字节数组(或16进制的字符串)转换的实现方 ...

  9. Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)

    一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...

随机推荐

  1. 20145236 冯佳 《Java程序设计》第1周学习总结

    20145236 冯佳 <Java程序设计>第1周学习总结 教材学习内容总结 因为假期在家的时候并没有提前自学Java,所以,这周算是真正开始第一次接触Java.我对Java的了解也仅仅停 ...

  2. spring关于urlpattern

    视图解析器(ViewResolver)注册中央调度器定制处理器jsp页面搭建springmvc.xml配置效果图第一个案例提升----视图解析器关于urlpattern说法最好配成*.do 不能配成/ ...

  3. 石子归并问题(nyoj737)

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的 ...

  4. [转]CentOS更改yum源与更新系统

    [1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...

  5. 如何采集所有QQ群成员?

    首先,你需要有一个CHROME浏览器其实,你要装一个叫REGEX SCRAPER的插件 在qun.qzone.qq.com打开你的QQ群页面-查看群成员 点击REGEX 插件, 粘贴上这个代码 tex ...

  6. 统计查询-sql

    select --总注册人数(select COUNT(*) from [YYD_Users_RegInfo]) as TotalCount,--pc端注册人数(select COUNT(*) fro ...

  7. excel的变量

    因需要定制游戏的公式,公式是以一个系数乘以等级,我想达到修改系数,每个等级对应的值就立即显示出来, 但把系数写在一个单元格,一拉,系数单元格也会跟着增长行数--不是我想要的: 但只要把系数单元格改成变 ...

  8. linux内核启动笔记

    一. 1.解压    tar xjf linux-2.6.22.6.tar.bz2 2.打补丁  patch -p1 < ../linux-2.6.22.6_jz2440.patch 3.配置 ...

  9. android listview综合使用示例_结合数据库操作和listitem单击长按等事件处理

    本示例说明: 1.自定义listview条目样式,自定义listview显示列数的多少,灵活与数据库中字段绑定. 2.实现对DB的增删改查,并且操作后listview自动刷新. 3.响应用户操作点击事 ...

  10. 经典线程同步 互斥量Mutex

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇一个经典的多线程同步问题> <秒杀多线程第五篇经典线程同步关键段CS> <秒杀多线程第六篇经典线程同步事件Event& ...