ポインタを使ってバイト配列をコピーします.
どうですか?ポインタを使ってバイト配列をコピーします.
以下の例では、ポインタを使用して、バイトを1つの配列から別のポインタを使用する配列にコピーします.
この例はunsafeキーワードを使用しており、
例:
同前 //comple with:/unsafe
同前 class TestCopy{ //The unsafe keyword allows pointers to be used within the following method: static unsafe void Copy(byte[]src,int src Index,byte[]dst,int dstIndex,int count) { if(src==null𞓜src Index<0|124; dst==null|dstIndex<0|count<0) { throw new System.AgmentException() }
int srcure=src.Length; int dstLen=dst.Length; if(srcurin-src Index //The follwing fixed statement pins the location of the src and dst object s //in memory so that they will not be moved by garbage collection. fixed(byte*pSrc=src,pDst=dst) { byte*ps=pSrc; byte*pd=pDst
//Loop over the count in blocks of 4 bytes、copying an integer(4 bytes)at a time: for(int i=0;i //Coplete the copy by moving any bytes that wereen't moved in blocks of 4: for(int i=0;i static void Main() { byte[]a=new byte[100]; byte[]b=new byte[100];
for(int i=0;i<100;+i) { a[i]=(byte)i }
Copy(a,0,b,0,100) System.sone.WriteLine(「The first 10 elements are:」)
for(int i=0;i<10;+i) { System.C.solie.Write(b[i]+「」); } System.sono sone.WriteLine(“/n”); }}
出力 The first 10 elements are:0 1 2 3 4 5 6 8 9
タスク「安全でないコード」の例を参照してください.
参照/unsafe(不安全モードを有効にしてください)(C〓コンパイラオプション)
概念C((zhiプログラミングガイドの安全でないコードとポインター(C〓プログラミングガイド)
その他資源ごみの回収
以下の例では、ポインタを使用して、バイトを1つの配列から別のポインタを使用する配列にコピーします.
この例はunsafeキーワードを使用しており、
Copy
方法でポインタを使用することができる.fixed文はソース配列とターゲット配列を示すポインタを表します.これはソース配列とターゲット配列のメモリ内の位置をロックし、ゴミ回収操作のために移動しないようにします.これらのメモリブロックは、fixedブロックの終了時にロックを解除します.この例ではCopy関数はunsafeキーワードを使用しているので、unsafeコンパイラオプションを使ってコンパイルしなければなりません.例:
同前 //comple with:/unsafe
同前 class TestCopy{ //The unsafe keyword allows pointers to be used within the following method: static unsafe void Copy(byte[]src,int src Index,byte[]dst,int dstIndex,int count) { if(src==null𞓜src Index<0|124; dst==null|dstIndex<0|count<0) { throw new System.AgmentException() }
int srcure=src.Length; int dstLen=dst.Length; if(srcurin-src Index
//Loop over the count in blocks of 4 bytes、copying an integer(4 bytes)at a time: for(int i=0;i
for(int i=0;i<100;+i) { a[i]=(byte)i }
Copy(a,0,b,0,100) System.sone.WriteLine(「The first 10 elements are:」)
for(int i=0;i<10;+i) { System.C.solie.Write(b[i]+「」); } System.sono sone.WriteLine(“/n”); }}
出力 The first 10 elements are:0 1 2 3 4 5 6 8 9
タスク「安全でないコード」の例を参照してください.
参照/unsafe(不安全モードを有効にしてください)(C〓コンパイラオプション)
概念C((zhiプログラミングガイドの安全でないコードとポインター(C〓プログラミングガイド)
その他資源ごみの回収