C#固定大小緩沖區(qū)及使用指針復(fù)制數(shù)據(jù)詳解
固定大小的緩沖區(qū) - Fixed-size buffers
可以使用 fixed 關(guān)鍵字創(chuàng)建在數(shù)據(jù)結(jié)構(gòu)中具有固定大小的數(shù)組的緩沖區(qū)(buffer)。當(dāng)編寫與其他語(yǔ)言或平臺(tái)的數(shù)據(jù)源進(jìn)行互操作的方法時(shí),固定大小的緩沖區(qū)很有用。
在可以使用常規(guī)結(jié)構(gòu)成員(regular struct members)的任何屬性或修飾符上都可以使用fixed-size buffer。
唯一的限制是數(shù)組類型必須為基礎(chǔ)類型 bool、byte、char、short、int, long、sbyte、ushort、uint、ulong、float 或 double。
比如:
private fixed char name[30];
在安全代碼中,包含數(shù)組的C#結(jié)構(gòu)不會(huì)包含任何數(shù)組元素,結(jié)構(gòu)只是包含到元素的引用。
在unsafe代碼塊中,可以為struct添加固定大小的數(shù)組。
下面 PathArray struct 的大小不取決于數(shù)組元素的數(shù)量,因?yàn)槭?code>pathName引用變量:
public struct PathArray
{
public char[] pathName;
private int reserved;
}
不安全代碼中,struct結(jié)構(gòu)可以包含嵌入的數(shù)組。下面的示例中,fixedBuffer數(shù)組有一個(gè)固定大小,使用fixed語(yǔ)句獲取第一個(gè)元素的指針,并通過(guò)該指針訪問(wèn)數(shù)組元素。fixed語(yǔ)句pin實(shí)例字段fixedBuffer到內(nèi)存中的一個(gè)特定位置。
internal unsafe struct Buffer
{
public fixed char fixedBuffer[128];
}
internal unsafe class Example
{
public Buffer buffer = default;
}
private static void AccessEmbeddedArray()
{
var example = new Example();
unsafe
{
// Pin the buffer to a fixed location in memory.
fixed (char* charPtr = example.buffer.fixedBuffer)
{
*charPtr = 'A';
}
// Access safely through the index:
char c = example.buffer.fixedBuffer[0];
Console.WriteLine(c);
// Modify through the index:
example.buffer.fixedBuffer[0] = 'B';
Console.WriteLine(example.buffer.fixedBuffer[0]);
}
}
128個(gè)元素的char數(shù)組大小是256個(gè)字節(jié)。固定大小的char緩沖區(qū)中每個(gè)字符固定占兩個(gè)字節(jié),不管采用哪種編碼。
數(shù)組的大小是相同的,即使char緩沖區(qū)被封送到 API 方法或結(jié)構(gòu)struct設(shè)置為CharSet = CharSet.Auto / CharSet = CharSet.Ansi
固定大小的bool數(shù)組,其元素占用的大小固定為1字節(jié)。bool數(shù)組不適合創(chuàng)建位數(shù)組或緩沖區(qū)。
固定大小的緩沖區(qū)使用 System.Runtime.CompilerServices.UnsafeValueTypeAttribute 進(jìn)行編譯,指示公共語(yǔ)言運(yùn)行時(shí) (CLR) 某個(gè)類型包含可能溢出的非托管數(shù)組。
使用 stackalloc 分配的內(nèi)存還會(huì)在 CLR 中自動(dòng)啟用緩沖區(qū)溢出檢測(cè)功能。
unsafe struct 中可以使用固定大小的緩沖區(qū)。比如前面的Buffer:
internal unsafe struct Buffer
{
public fixed char fixedBuffer[128];
}
編譯器會(huì)生成下面帶特性的Buffer:
internal struct Buffer
{
[StructLayout(LayoutKind.Sequential, Size = 256)]
[CompilerGenerated]
[UnsafeValueType]
public struct <fixedBuffer>e__FixedBuffer
{
public char FixedElementField;
}
[FixedBuffer(typeof(char), 128)]
public <fixedBuffer>e__FixedBuffer fixedBuffer;
}
固定大小的緩沖區(qū)不同于常規(guī)數(shù)組的幾個(gè)方式如下:
- 只能在
unsafe上下文中使用 - 只能作為結(jié)構(gòu)的實(shí)例字段
- 始終是矢量或一維數(shù)組。
- 聲明應(yīng)包括長(zhǎng)度,如
fixed char id[8],不能使用fixed char id[]。
如何使用指針復(fù)制字節(jié)數(shù)組
下面的通過(guò)unsafe關(guān)鍵字使Copy方法中可以使用指針;fixed語(yǔ)句用于聲明source和destination數(shù)組的指針。
fixed語(yǔ)句pin源和目標(biāo)數(shù)組在內(nèi)存中的位置不被垃圾收集移動(dòng)。當(dāng)fixed塊執(zhí)行結(jié)束后,數(shù)組的內(nèi)存塊會(huì)解除固定(unpinned)。
該示例使用索引(indices-指標(biāo))而不是第二個(gè)非托管指針訪問(wèn)兩個(gè)數(shù)組的元素。pSource 和 pTarget 指針的聲明固定數(shù)組。
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("source or target is null");
}
// 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("offset or bytes to copy is negative");
}
// 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("offset to end of array is less than bytes to be copied");
}
// 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)
{
// Copy the specified number of bytes from source to target.
for (int i = 0; i < count; i++)
{
pTarget[targetOffset + i] = pSource[sourceOffset + i];
}
}
}
參考
以上就是C#固定大小緩沖區(qū)及使用指針復(fù)制數(shù)據(jù)詳解的詳細(xì)內(nèi)容,更多關(guān)于C# 固定大小緩沖區(qū)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#中object.ReferenceEquals方法的使用
本文詳細(xì)解釋了C#中的object.ReferenceEquals方法,用于判斷兩個(gè)對(duì)象是否引用同一內(nèi)存地址,通過(guò)示例展示了方法的使用及其在確定對(duì)象引用關(guān)系時(shí)的重要性,感興趣的可以了解一下2025-07-07
C#使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件
這篇文章主要為大家詳細(xì)介紹了C#如何使用百度Ueditor富文本框?qū)崿F(xiàn)上傳文件(圖片,視頻等),文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-07-07
基于C#實(shí)現(xiàn)串口監(jiān)聽與TCP轉(zhuǎn)發(fā)功能
在工業(yè)自動(dòng)化、物聯(lián)網(wǎng)設(shè)備管理、遠(yuǎn)程監(jiān)控等應(yīng)用場(chǎng)景中,經(jīng)常需要將本地串口設(shè)備的數(shù)據(jù)實(shí)時(shí)傳輸?shù)竭h(yuǎn)程服務(wù)器進(jìn)行處理,本文將詳細(xì)介紹如何使用 C# 創(chuàng)建一個(gè)串口監(jiān)聽服務(wù),并將接收到的數(shù)據(jù)通過(guò) TCP 協(xié)議轉(zhuǎn)發(fā)至遠(yuǎn)程服務(wù)器,需要的朋友可以參考下2025-06-06
C#?wpf使用DockPanel實(shí)現(xiàn)制作截屏框
做桌面客戶端的時(shí)候有時(shí)需要實(shí)現(xiàn)截屏功能,能夠在界面上框選截屏,本文就來(lái)為大家介紹一下wpf如何使用DockPanel制作截屏框吧,感興趣的可以了解下2023-09-09
Unity UGUI的LayoutElement布局元素組件介紹使用示例
這篇文章主要為大家介紹了Unity UGUI的LayoutElement布局元素組件介紹使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
利用WinForm實(shí)現(xiàn)上左右布局的方法詳解
現(xiàn)在90%的管理系統(tǒng)都是在用上左右這種布局方式,真可謂是經(jīng)典永流傳。本文將利用WinForm實(shí)現(xiàn)上左右布局這一布局效果,感興趣的可以學(xué)習(xí)一下2022-09-09
Unity UGUI的Slider滑動(dòng)條件組使用詳解
這篇文章主要為大家介紹了Unity UGUI的Slider滑動(dòng)條件組使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

