首先,我們的環境的基本信息:我們在Win7-x64上使用c#.net 4.0,目標是32位。我們有一個預分配的大數組。在一個函數中,我們想返回一個指向這個數組中任意點的指針,這樣調用函數就可以知道在哪裏寫入。例:如何獲取指向c中數組中間的指針#
class SomeClass {
void function_that_uses_the_array() {
Byte [] whereToWrite = getEmptyPtrLocation(1200);
Array.Copy(sourceArray, whereToWrite, ...);
}
}
class DataProvider {
int MAX_SIZE = 1024*1024*64;
Byte [] dataArray = new Byte[MAX_SIZE];
int emptyPtr=0;
Byte[] getEmptyPtrLocation(int requestedBytes) {
int ref = emptyPtr;
emptyPtr += requestedBytes;
return dataArray[ref];
}
}
本質上,我們要預先分配的存儲器一大塊,並且該存儲器塊的儲備任意長度的部分,並讓其他一些類/函數使用的存儲器的部分。
在上例中,getEmptyPtrLocation函數不正確;它被聲明爲返回字節[],但試圖返回單個字節值。
感謝
爲什麼要那樣做? – Femaref 2011-02-02 15:09:18