Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Update unsafe code doc #47124

wants to merge 6 commits into from

Conversation

richlander
Copy link
Member

@richlander richlander commented Jul 7, 2025

Updating unsafe code doc to reflect our current thinking.

Related:

@EgorBo


Internal previews

📄 File 🔗 Preview link
docs/csharp/language-reference/unsafe-code.md Unsafe code

@dotnetrepoman dotnetrepoman bot added this to the July 2025 milestone Jul 7, 2025
@richlander richlander changed the title Update intro sections Update unsafe code doc Jul 7, 2025
@richlander richlander marked this pull request as ready for review July 10, 2025 17:03
@richlander richlander requested review from BillWagner and a team as code owners July 10, 2025 17:03
Copy link
Member

@BillWagner BillWagner left a 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
Copy link
Member

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
Copy link
Member

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"

Suggested change
- `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).
Copy link
Member

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.
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants