-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
docs/document/Unsafe CSharp/docs/Fundamentals/8. Pointer Array.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
# Pointer Array | ||
|
||
```cs | ||
int** p = stackalloc int*[30]; | ||
int*** pp = stackalloc int**[10]; | ||
unsafe | ||
{ | ||
int* p = stackalloc int[10]; | ||
int** pp = stackalloc int*[10]; | ||
int*** ppp = stackalloc int**[10]; | ||
int**** pppp = stackalloc int***[10]; | ||
} | ||
``` |
88 changes: 88 additions & 0 deletions
88
docs/document/Unsafe CSharp/docs/Fundamentals/9. Function Pointer.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Function Pointer | ||
|
||
> Function pointer can only be created by referencing a static method. | ||
:::info Reference | ||
See: [Function pointers](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/unsafe-code#function-pointers) | ||
::: | ||
|
||
## Declaration | ||
|
||
```cs | ||
delegate*<void> f = &M; | ||
delegate*<int, void> g = &N; | ||
static void M() { } | ||
static void N(int a) { } | ||
``` | ||
|
||
## Invocation | ||
|
||
A function pointer is a pure reference to the function without the delegate wrapper. | ||
There's no member of it to be accessed using `.`, can only be invoked by `()`; | ||
|
||
```cs{3} | ||
delegate*<void> f = &M; | ||
delegate*<int, void> g = &N; | ||
f(); g(default); | ||
static void M() { } | ||
static void N(int a) { } | ||
``` | ||
|
||
## Type parameter variant | ||
|
||
```cs{2,4} | ||
delegate*<int, int, int> h = &O; | ||
delegate*<char*, int*, int*> i = &P; | ||
static int O(int a, int b) => default; | ||
static unsafe int* P(void* m, int* b) => default; | ||
``` | ||
|
||
## Operator | ||
|
||
```cs | ||
|
||
``` | ||
|
||
## Calling Conventions | ||
|
||
### `managed` | ||
|
||
- `managed` is the default calling convention of a function pointer. | ||
|
||
```cs | ||
delegate*<void> f = &M; | ||
delegate* managed<void> g = &M; | ||
static void M() { } | ||
``` | ||
|
||
### `unmanaged` | ||
|
||
- `Cdecl` | ||
|
||
- `Stdcall` | ||
|
||
- `Fastcall` | ||
|
||
- `Thiscall` | ||
|
||
- Default `unmanaged` calling convention | ||
|
||
The default `unmanaged` calling convention is left to CLR to make the decision. | ||
|
||
```cs | ||
public unsafe static void M<T>(delegate* unmanaged<T> f) { } | ||
``` | ||
|
||
## Type Checking | ||
|
||
Type checking of two function pointer includes function signatures and their calling convention. | ||
|
||
```cs | ||
delegate*<void> f = &M; | ||
delegate* managed<void> g = &N; | ||
delegate* unmanaged<void> h = ...; | ||
f = g; | ||
g = h; // calling convention not matched! // [!code error] | ||
static void M() { } | ||
static void N() { } | ||
``` |