Skip to content
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

Update sizeof and pointer type to keep up to date with C# 11, plus some fixes/improvements #44137

Open
GeeLaw opened this issue Jan 2, 2025 · 2 comments
Assignees
Labels
dotnet-csharp/svc lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3

Comments

@GeeLaw
Copy link

GeeLaw commented Jan 2, 2025

Type of issue

Outdated article

Description

Part 1: sizeof

In sizeof.md.

The argument to the sizeof operator must be the name of an unmanaged type or a type parameter that is constrained to be an unmanaged type.

The constraint of unmanaged type is no longer present in C# 11. Suggested rewording:

The argument to the sizeof operator must be the name of a type (including ones constructed using generic type parameters).

Then in the last paragraph, deal with the size of reference types. I also made the explanation of struct types consistent with the suggested addition.

== The sizeof operator returns a number of bytes that would be allocated by the common language runtime in managed memory.
++ For reference types, their size is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms. This is not the size of an instance of that type, but just the size of a reference. Indeed, a variable of type object takes 4 or 8 bytes, not the amount of memory occupied by an object instance.
-- For struct types, that value includes any padding, as the preceding example demonstrates.
++ For value types, their size is the size of an instance, including any padding, as the preceding example demonstrates.
== The result of the sizeof operator might differ from the result of the Marshal.SizeOf method, which returns the size of a type in unmanaged memory.

Part 2: Pointer Types

In unsafe-code.md.

The type specified before the * in a pointer type is called the referent type.
Only an unmanaged type can be a referent type.
...
A pointer can't point to a reference or to a struct that contains references, because an object reference can be garbage collected even if a pointer is pointing to it.
The garbage collector doesn't keep track of whether an object is being pointed to by any pointer types.

The second sentence is outdated in C# 11. The third sentence has always been logically incorrect. The last sentence states a problem, but without immediately offering a solution. Suggested rewording:

== The type specified before the * in a pointer type is called the referent type.
-- Only an unmanaged type can be a referent type.
...
-- A pointer can't ... pointing to it.
== The garbage collector doesn't keep track of whether an object is being pointed to by any pointer types.
++ If a pointer points into an object in the managed heap (including local variables captured by lambda expressions or anonymous delegates), the object must be pinned for as long as the pointer is used. [Copy a link to the fixed statement, which appears later in the same documentation article.]

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/operators/sizeof.md

Document Version Independent Id

7e28a662-f550-d202-9a97-e684ae1b074d

Article author

@BillWagner

Metadata

  • ID: 191acdd8-8e36-8bd4-1172-140717c36a85
  • Service: dotnet-csharp
  • Sub-service: lang-reference

Related Issues

@GeeLaw
Copy link
Author

GeeLaw commented Jan 2, 2025

Linking: dotnet/csharplang#8953

@dotnet-policy-service dotnet-policy-service bot added the ⌚ Not Triaged Not triaged label Jan 2, 2025
@BillWagner BillWagner self-assigned this Jan 6, 2025
@BillWagner BillWagner added Pri1 High priority, do before Pri2 and Pri3 and removed ⌚ Not Triaged Not triaged labels Jan 6, 2025
@dotnetrepoman dotnetrepoman bot added the 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. label Jan 6, 2025
@dotnet-policy-service dotnet-policy-service bot removed the 🗺️ mapQUEST Only used as a way to mark an issue as updated for quest. RepoMan should instantly remove it. label Jan 6, 2025
@BillWagner
Copy link
Member

Hi @GeeLaw

Thanks for doing all the research on this to note the proper updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-csharp/svc lang-reference/subsvc Pri1 High priority, do before Pri2 and Pri3
Projects
Development

No branches or pull requests

2 participants