-
Notifications
You must be signed in to change notification settings - Fork 6k
Update unsafe code doc #47124
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
base: main
Are you sure you want to change the base?
Update unsafe code doc #47124
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this is a good improvement. I had a few comments we should address before merging.
type* identifier; | ||
void* identifier; //allowed but not recommended |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is legal, and this is the reference, so we should leave this here.
There are several examples of pointers in the articles on the [`fixed` statement](statements/fixed.md). The following example uses the `unsafe` keyword and the `fixed` statement, and shows how to increment an interior pointer. You can paste this code into the Main function of a console application to run it. These examples must be compiled with the [**AllowUnsafeBlocks**](compiler-options/language.md#allowunsafeblocks) compiler option set. | ||
- `int* p`: pointer to `int` | ||
- `int** p`: pointer to pointer to `int` | ||
- `int*[] p`: array of `int` pointers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, I'd use "pointers to int
"
- `int*[] p`: array of `int` pointers | |
- `int*[] p`: array of pointers to `int` |
|
||
You can't apply the indirection operator to a pointer of type `void*`. However, you can use a cast to convert a void pointer to any other pointer type, and vice versa. | ||
Pointers don't inherit from [`object`](builtin-types/reference-types.md). You can't box or unbox pointers, and there's no conversion between pointers and `object`. However, you can cast between pointer types and between pointers and integral types (with an explicit cast). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And, we do need to add void*
conversions (reference), with appropriate "danger, not recommended" language.
|
||
:::code language="csharp" source="snippets/unsafe-code/Conversions.cs" ID="Conversion"::: | ||
|
||
### Pointer safety reminders | ||
|
||
- Dereferencing a null pointer is implementation-defined and may crash your program. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is the language reference for our implementation, we should say what we do on all platforms here.
I'll defer to others, but I think it's consistent, but platform dependent. The null
value is defined as all 0's. IIRC, all platforms we support have an OS level exception for that operation.
Updating unsafe code doc to reflect our current thinking.
Related:
@EgorBo
Internal previews