Skip to content

Commit 919e06d

Browse files
committed
Correct fixed size buffers article
1 parent 1be56f4 commit 919e06d

File tree

2 files changed

+43
-18
lines changed

2 files changed

+43
-18
lines changed

docs/csharp/programming-guide/unsafe-code-pointers/fixed-size-buffers.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Fixed Size Buffers - C# Programming Guide"
3-
ms.date: 04/20/2018
4-
helpviewer_keywords:
3+
ms.date: 04/23/2020
4+
helpviewer_keywords:
55
- "fixed size buffers [C#]"
66
- "unsafe buffers [C#]"
77
- "unsafe code [C#], fixed size buffers"
@@ -32,15 +32,39 @@ The preceding example demonstrates accessing `fixed` fields without pinning, wh
3232

3333
Another common fixed-size array is the [bool](../../language-reference/builtin-types/bool.md) array. The elements in a `bool` array are always one byte in size. `bool` arrays are not appropriate for creating bit arrays or buffers.
3434

35-
> [!NOTE]
36-
> Except for memory created by using [stackalloc](../../language-reference/operators/stackalloc.md), the C# compiler and the common language runtime (CLR) do not perform any security buffer overrun checks. As with all unsafe code, use caution.
35+
Fixed size buffers are compiled with the <xref:System.Runtime.CompilerServices.UnsafeValueTypeAttribute?displayProperty=nameWithType>, which instructs the common language runtime (CLR) that a type contains an unmanaged array that can potentially overflow. This is similar to memory created using [stackalloc](../../language-reference/operators/stackalloc.md), which automatically enables buffer overrun detection features in the CLR. The previous example shows how a fixed size buffer could exist in an `unsafe struct`.
3736

38-
Unsafe buffers differ from regular arrays in the following ways:
37+
```csharp
38+
internal unsafe struct Buffer
39+
{
40+
public fixed char fixedBuffer[128];
41+
}
42+
```
43+
44+
The compiler generated C# for `Buffer`, is attributed as follows:
45+
46+
```csharp
47+
internal struct Buffer
48+
{
49+
[StructLayout(LayoutKind.Sequential, Size = 256)]
50+
[CompilerGenerated]
51+
[UnsafeValueType]
52+
public struct <fixedBuffer>e__FixedBuffer
53+
{
54+
public char FixedElementField;
55+
}
56+
57+
[FixedBuffer(typeof(char), 128)]
58+
public <fixedBuffer>e__FixedBuffer fixedBuffer;
59+
}
60+
```
61+
62+
Fixed size buffers differ from regular arrays in the following ways:
3963

40-
- You can only use unsafe buffers in an unsafe context.
41-
- Unsafe buffers are always vectors, or one-dimensional arrays.
42-
- The declaration of the array should include a count, such as `char id[8]`. You cannot use `char id[]`.
43-
- Unsafe buffers can only be instance fields of structs in an unsafe context.
64+
- May only be used in an [unsafe](../../language-reference/keywords/unsafe.md) context.
65+
- May only be instance fields of structs.
66+
- They're always vectors, or one-dimensional arrays.
67+
- The declaration should include the length, such as `fixed char id[8]`. You cannot use `fixed char id[]`.
4468

4569
## See also
4670

samples/snippets/csharp/keywords/FixedKeywordExamples.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -192,33 +192,34 @@ public struct PathArray
192192
// </Snippet6>
193193

194194
// <Snippet7>
195-
internal unsafe struct MyBuffer
195+
internal unsafe struct Buffer
196196
{
197197
public fixed char fixedBuffer[128];
198198
}
199199

200-
internal unsafe class MyClass
200+
internal unsafe class Example
201201
{
202-
public MyBuffer myBuffer = default;
202+
public Buffer buffer = default;
203203
}
204204

205205
private static void AccessEmbeddedArray()
206206
{
207-
MyClass myC = new MyClass();
207+
var example = new Example();
208208

209209
unsafe
210210
{
211211
// Pin the buffer to a fixed location in memory.
212-
fixed (char* charPtr = myC.myBuffer.fixedBuffer)
212+
fixed (char* charPtr = example.buffer.fixedBuffer)
213213
{
214214
*charPtr = 'A';
215215
}
216216
// Access safely through the index:
217-
char c = myC.myBuffer.fixedBuffer[0];
217+
char c = example.buffer.fixedBuffer[0];
218218
Console.WriteLine(c);
219-
// modify through the index:
220-
myC.myBuffer.fixedBuffer[0] = 'B';
221-
Console.WriteLine(myC.myBuffer.fixedBuffer[0]);
219+
220+
// Modify through the index:
221+
example.buffer.fixedBuffer[0] = 'B';
222+
Console.WriteLine(example.buffer.fixedBuffer[0]);
222223
}
223224
}
224225
// </Snippet7>

0 commit comments

Comments
 (0)