Skip to content

Example metadata: unsafe-code.md #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions standard/unsafe-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ unsafe_statement

> *Example*: In the following code
>
> <!-- Example: {template:"standalone-lib", name:"UnsafeModifierOnStruct"} -->
> <!-- Example: {template:"standalone-lib-without-using", name:"UnsafeModifierOnStruct"} -->
> ```csharp
> public unsafe struct Node
> {
Expand All @@ -51,7 +51,7 @@ unsafe_statement
>
> the `unsafe` modifier specified in the struct declaration causes the entire textual extent of the struct declaration to become an unsafe context. Thus, it is possible to declare the `Left` and `Right` fields to be of a pointer type. The example above could also be written
>
> <!-- Example: {template:"standalone-lib", name:"UnsafeContexts2"} -->
> <!-- Example: {template:"standalone-lib-without-using", name:"UnsafeContexts2"} -->
> ```csharp
> public struct Node
> {
Expand All @@ -69,7 +69,7 @@ Other than establishing an unsafe context, thus permitting the use of pointer ty

> *Example*: In the following code
>
> <!-- Example: {template:"standalone-lib", name:"UnsafeContexts3", replaceEllipsis:true, ignoredWarnings:["CS0168"]} -->
> <!-- Example: {template:"standalone-lib-without-using", name:"UnsafeContexts3", replaceEllipsis:true, ignoredWarnings:["CS0168"]} -->
> ```csharp
> public class A
> {
Expand All @@ -94,7 +94,7 @@ Other than establishing an unsafe context, thus permitting the use of pointer ty
>
> The situation is slightly different when a pointer type is part of the method’s signature
>
> <!-- Example: {template:"standalone-lib", name:"UnsafeContexts4", replaceEllipsis:true} -->
> <!-- Example: {template:"standalone-lib-without-using", name:"UnsafeContexts4", replaceEllipsis:true} -->
> ```csharp
> public unsafe class A
> {
Expand Down Expand Up @@ -174,10 +174,8 @@ A *pointer_type* may be used as the type of a volatile field ([§14.5.4](classes

> *Note*: Although pointers can be passed as `ref` or `out` parameters, doing so can cause undefined behavior, since the pointer might well be set to point to a local variable that no longer exists when the called method returns, or the fixed object to which it used to point, is no longer fixed. For example:
>
> <!-- UndefinedExample: {template:"standalone-console", name:"PointerTypes1", replaceEllipsis:true, expectedOutput:["*px1 = 10","*px2 = 20"], expectedWarnings:["x","x"]} -->
> <!-- Undefined$Example: {template:"standalone-console", name:"PointerTypes1", replaceEllipsis:true, expectedOutput:["*px1 = 10","*px2 = 20"], expectedWarnings:["x","x"]} -->
> ```csharp
> using System;
>
> class Test
> {
> static int value = 20;
Expand Down Expand Up @@ -214,7 +212,7 @@ A method can return a value of some type, and that type can be a pointer.

> *Example*: When given a pointer to a contiguous sequence of `int`s, that sequence’s element count, and some other `int` value, the following method returns the address of that value in that sequence, if a match occurs; otherwise it returns `null`:
>
> <!-- Example: {template:"standalone-console", name:"PointerTypes2", expectedWarnings:["CS8321"]} -->
> <!-- Example: {template:"standalone-console-without-using", name:"PointerTypes2", expectedWarnings:["CS8321"]} -->
> ```csharp
> unsafe static int* Find(int* pi, int size, int value)
> {
Expand Down Expand Up @@ -288,7 +286,7 @@ When one pointer type is converted to another, if the resulting pointer is not c

> *Example*: Consider the following case in which a variable having one type is accessed via a pointer to a different type:
>
> <!-- UndefinedExample: {template:"standalone-console", name:"PointerConversions1", expectedWarnings:["CS8321"]} -->
> <!-- Undefined$Example: {template:"standalone-console-without-using", name:"PointerConversions1", expectedWarnings:["CS8321"]} -->
> ```csharp
> unsafe static void M()
> {
Expand All @@ -307,9 +305,8 @@ When a pointer type is converted to a pointer to `byte`, the result points to th

> *Example*: The following method displays each of the eight bytes in a `double` as a hexadecimal value:
>
> <!-- ImplementationDefinedExample: {template:"standalone-console", name:"PointerConversions2", expectedOutput:["BA","FF","51","A2","90","6C","24","45"], expectedErrors:["x","x"]} -->
> <!-- ImplementationDefined$Example: {template:"standalone-console", name:"PointerConversions2", expectedOutput:["BA","FF","51","A2","90","6C","24","45"], expectedErrors:["x","x"]} -->
> ```csharp
> using System;
> class Test
> {
> static void Main()
Expand Down Expand Up @@ -420,9 +417,8 @@ A pointer member access of the form `P->I` is evaluated exactly as `(*P).I`. For
> *Example*: In the following code
>
> <!-- Example: {template:"standalone-console", name:"PointerMemberAccess1", expectedOutput:["(10,20)"]} -->
> <!-- Maintenance Note: A version of this type exists in additional-files as "PointStructWithToString.cs". As such, certain changes to this type definition might need to be reflected in that file, in which case, *all* examples using that file should be tested. -->
> ```csharp
> using System;
>
> struct Point
> {
> public int x;
Expand All @@ -448,7 +444,7 @@ A pointer member access of the form `P->I` is evaluated exactly as `(*P).I`. For
>
> the `->` operator is used to access fields and invoke a method of a struct through a pointer. Because the operation `P->I` is precisely equivalent to `(*P).I`, the `Main` method could equally well have been written:
>
> <!-- IncompleteExample: {template:"standalone-console", name:"PointerMemberAccess2", expectedOutput:["(10,20)"]} -->
> <!-- Example: {template:"standalone-console", name:"PointerMemberAccess2", additionalFiles:["PointStructWithToString.cs"], expectedOutput:["(10,20)"]} -->
> ```csharp
> class Test
> {
Expand Down Expand Up @@ -484,7 +480,7 @@ A pointer element access of the form `P[E]` is evaluated exactly as `*(P + E)`.

> *Example*: In the following code
>
> <!-- Example: {template:"standalone-console", name:"PointerElementAccess1"} -->
> <!-- Example: {template:"standalone-console-without-using", name:"PointerElementAccess1"} -->
> ```csharp
> class Test
> {
Expand All @@ -504,7 +500,7 @@ A pointer element access of the form `P[E]` is evaluated exactly as `*(P + E)`.
>
> a pointer element access is used to initialize the character buffer in a `for` loop. Because the operation `P[E]` is precisely equivalent to `*(P + E)`, the example could equally well have been written:
>
> <!-- Example: {template:"standalone-console", name:"PointerElementAccess2"} -->
> <!-- Example: {template:"standalone-console-without-using", name:"PointerElementAccess2"} -->
> ```csharp
> class Test
> {
Expand Down Expand Up @@ -548,8 +544,6 @@ The `&` operator does not require its argument to be definitely assigned, but fo
>
> <!-- Example: {template:"standalone-console", name:"Address-ofOperator", expectedOutput:["123"]} -->
> ```csharp
> using System;
>
> class Test
> {
> static void Main()
Expand Down Expand Up @@ -618,7 +612,6 @@ Given two expressions, `P` and `Q`, of a pointer type `T*`, the expression `P
>
> <!-- Example: {template:"standalone-console", name:"PointerArithmetic", inferOutput:true} -->
> ```csharp
> using System;
> class Test
> {
> static void Main()
Expand Down Expand Up @@ -715,7 +708,7 @@ Fixed objects can cause fragmentation of the heap (because they can’t be moved

> *Example*: The example
>
> <!-- Example: {template:"standalone-console", name:"FixedStatement1"} -->
> <!-- Example: {template:"standalone-console-without-using", name:"FixedStatement1"} -->
> ```csharp
> class Test
> {
Expand Down Expand Up @@ -756,8 +749,6 @@ Within a `fixed` statement that obtains a pointer `p` to an array instance `a`,
>
> <!-- Example: {template:"standalone-console", name:"FixedStatement2", inferOutput:true} -->
> ```csharp
> using System;
>
> class Test
> {
> static void Main()
Expand Down Expand Up @@ -806,7 +797,7 @@ Within a `fixed` statement that obtains a pointer `p` to an array instance `a`,

> *Example*: In the following code
>
> <!-- Example: {template:"standalone-console", name:"FixedStatement3"} -->
> <!-- Example: {template:"standalone-console-without-using", name:"FixedStatement3"} -->
> ```csharp
> class Test
> {
Expand Down Expand Up @@ -927,7 +918,7 @@ A fixed-size buffer declaration that declares multiple fixed-size buffers is equ

> *Example*:
>
> <!-- Example: {template:"standalone-lib", name:"Fixed-sizeBuffers1"} -->
> <!-- Example: {template:"standalone-lib-without-using", name:"Fixed-sizeBuffers1"} -->
> ```csharp
> unsafe struct A
> {
Expand All @@ -937,7 +928,7 @@ A fixed-size buffer declaration that declares multiple fixed-size buffers is equ
>
> is equivalent to
>
> <!-- Example: {template:"standalone-lib", name:"Fixed-sizeBuffers2"} -->
> <!-- Example: {template:"standalone-lib-without-using", name:"Fixed-sizeBuffers2"} -->
> ```csharp
> unsafe struct A
> {
Expand Down Expand Up @@ -968,7 +959,7 @@ The subsequent elements of the fixed-size buffer can be accessed using pointer o

> *Example*: The following declares and uses a struct with a fixed-size buffer member.
>
> <!-- Example: {template:"standalone-console", name:"Fixed-sizeBuffersInExpressions"} -->
> <!-- Example: {template:"standalone-console-without-using", name:"Fixed-sizeBuffersInExpressions"} -->
> ```csharp
> unsafe struct Font
> {
Expand Down Expand Up @@ -1042,8 +1033,6 @@ All stack-allocated memory blocks created during the execution of a function mem
>
> <!-- Example: {template:"standalone-console", name:"StackAllocation", expectedOutput:["12345","-999"]} -->
> ```csharp
> using System;
>
> class Test
> {
> static string IntToString(int value)
Expand Down