【发布时间】:2011-02-02 15:07:41
【问题描述】:
首先,关于我们环境的基本信息:我们使用 c# .net 4.0,在 Win7-x64 上,针对 32 位。
我们有一个预先分配的 -large- 数组。在函数中,我们希望返回一个指向该数组中任意点的指针,以便调用函数知道在哪里写入。例如:
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 函数不正确;它被声明为返回 Byte[],但试图返回单个字节值。
谢谢
【问题讨论】:
标签: c# .net pointers bytearray memory-pool