-
Notifications
You must be signed in to change notification settings - Fork 6k
Operator new doesn't create structs on the stack #4667
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
Conversation
int i = 0; | ||
``` | ||
|
||
For a complete list of default values, see [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). | ||
|
||
Remember that it is an error to declare a default constructor for a [struct](../../../csharp/language-reference/keywords/struct.md) because every value type implicitly has a public default constructor. It is possible to declare parameterized constructors on a struct type to set its initial values, but this is only necessary if values other than the default are required. | ||
|
||
Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. For reference types that consume fixed resources such as large amounts of memory, file handles, or network connections, it is sometimes desirable to employ deterministic finalization to ensure that the object is destroyed as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md). | ||
Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, whereas reference-type objects are destroyed at an unspecified time after the last reference to them is removed. For reference types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources it contains are released as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md). |
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.
I'm not completely satisfied with this formulation (when do value types in methods with closures or in coroutines go "out of scope"?), so feel free to suggest how to improve this further.
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.
@svick @BillWagner this paragraph (in both edited and previous versions) triggers some questions:
- What does go out of scope mean for value-types?
- As for reference-type objects, the sentence that says ...after the last reference to them is removed. That might make one think about how to remove a reference to an object?
- Structs also might contain file handles (in general, implement
IDisposable
). What to do with them?
I'm afraid this paragraph might be still confusing. What was the original reason to write it? To say: while you construct with new
, do not care about clean-up as it's automatic in a lot of cases and remember about unmanaged resources?
Is it possible to shorten this paragraph into one sentence and give a link to the page about garbage collection as well?
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.
Structs also might contain file handles (in general, implement IDisposable). What to do with them?
You're right, even though structs with resources are fairly rare. Still, I have changed it so that it now talks about "types", not just "reference types".
Your remaining points are valid, but I don't know how to clarify the paragraph further. And I'm not sure shortening it to just a couple of links is the right solution either.
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.
@svick that's why I wonder for which problem we look for
the right solution
Imagine that paragraph were removed completely :) What would the docs miss then?
Though I go too far to the philosophy-land. As for now, the paragraph is clearly improved and worth merging.
int i = 0; | ||
``` | ||
|
||
For a complete list of default values, see [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). | ||
|
||
Remember that it is an error to declare a default constructor for a [struct](../../../csharp/language-reference/keywords/struct.md) because every value type implicitly has a public default constructor. It is possible to declare parameterized constructors on a struct type to set its initial values, but this is only necessary if values other than the default are required. | ||
|
||
Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. For reference types that consume fixed resources such as large amounts of memory, file handles, or network connections, it is sometimes desirable to employ deterministic finalization to ensure that the object is destroyed as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md). | ||
Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, whereas reference-type objects are destroyed at an unspecified time after the last reference to them is removed. For types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources they contain are released as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md). |
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.
Maybe should state that reference types references are managed by the GC?
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.
@jzeferino Yeah, good idea, added.
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 LGTM.
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.
Thanks for making this update, and sparking a truly enjoyable and complicated discussion 🤔
I like the way you've updated everything, I just have one small comment on wording to avoid making a guarantee that structs are always destroyed when they go out of scope. (they won't be if they are members of a class). Explaining those is out of scope, so I propose a correct, but incomplete re-wording.
int i = 0; | ||
``` | ||
|
||
For a complete list of default values, see [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md). | ||
|
||
Remember that it is an error to declare a default constructor for a [struct](../../../csharp/language-reference/keywords/struct.md) because every value type implicitly has a public default constructor. It is possible to declare parameterized constructors on a struct type to set its initial values, but this is only necessary if values other than the default are required. | ||
|
||
Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. For reference types that consume fixed resources such as large amounts of memory, file handles, or network connections, it is sometimes desirable to employ deterministic finalization to ensure that the object is destroyed as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md). | ||
Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, whereas reference-type objects are destroyed by the garbage collector at an unspecified time after the last reference to them is removed. For types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources they contain are released as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md). |
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 this topic, I'd be a bit more vague on value types. Where you say
value-type objects are destroyed when they go out of scope,
I'd say
value-type objects may be destroyed when the go out of scope,
Yes, I know that's not 100% accurate, and it does beg the question of "when?"
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.
I don't like the proposed wording, because it's too vague: it doesn't say almost anything.
What do you think about something like the following?
value-type objects are destroyed when their scope is destroyed,
Or maybe use a different term instead of "scope", like "containing context"?
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.
I like "containing context". That worked in all the instances I tried in a thought exercise this morning:
- local variables in normal routines
- member of struct
- member of class
- closures for lambda expressions, etc.
- local in co-routine
- static member of struct or class
Let's go with it.
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.
@BillWagner Done.
Thanks @svick This is an important start. I'll |
* Fixes and improvements to Semantic analysis Get started (dotnet#4575) * Order the definitions to match the order of terms (dotnet#4576) The introductory sentence could be read as implying definitions that contradict the body of the article. * Fix invalid Windows file system paths (dotnet#4559) * Fix invalid Windows file system paths * Add escape characters * Fixing capitalization of Visual Basic (dotnet#4578) Changed "Visual basic" to "Visual Basic" for the description of the Get Visual Basic link. * Fixed broken links in interop docs (dotnet#4573) * Fixed broken links in interop docs * Updated additional links * Removed 'en-us' from URLs * Fixing broken link to MVA course (dotnet#4580) Visual Basic Fundamentals for Absolute Beginners link was directing to a list of courses, not really related to VB. I've updated the link directly to the course. I did check to see if the course was in other languages before putting in the en-US link - looks like it's available only in English. * Minor refactoring (dotnet#4582) Change "new ValidationResult(true, null)" to "ValidationResult.ValidResult", since it looks a little cleaner and the property was explicitly designed for that use. * Fixed link (dotnet#4583) * Update "How to: Modify string" (dotnet#4519) * article and code moved * interim checkin Switching tasks. * checkin to run test build I want to see if I can use local functions in C# interactive. * finish samples and new draft Ready for my first review. * fix build errors * proofread * update per feedback. * respond to feedback. * additional feedback. * respond to feedback. * Fix escape characters in HttpListener Remarks section (dotnet#4495) * Minor refactoring (dotnet#4584) Change "New ValidationResult(True, Nothing)" to "ValidationResult.ValidResult", since it looks a little cleaner and the property was explicitly designed for that use. * fix title (dotnet#4590) * fix broken links (dotnet#4588) * Add the closing token for an XML include (dotnet#4593) Without this, the rest of the file renders incorrectly. * Correct description of ROOT parameter (dotnet#4595) * Update tuples.md "Interoperation with C# Tuples" content (dotnet#4581) * Removed an excessive "and" * Removed some text that didn't seem overly clear. * Replaced link to UserVoice (dotnet#4594) * Removed link to UserVoice * Changed URL target * Fix typo (dotnet#4599) * Experiment: CodeOwners (dotnet#4597) * Experiment: CodeOwners Trying an experiment to see how we might use use the CODEOWNERS file to get reviews routed to the right person. * Added several sections * Update CODEOWNERS * Fixing anchor link to the "private protected access modifier" section (dotnet#4601) * fixes to the CODEOWNERS (dotnet#4602) * fixes to the CODEOWNERS The changes here: 1. The '@' is needed on each user name. 1. The C# Snippets was missing the 'csharp' directory component. 1. Order is important. * Add F# guide. * modify-string-contents.md: added interactive note (dotnet#4607) * Fix markdown table (dotnet#4603) Fix usage of `|` in markdown (use `|` instead) * Update infrastructure-persistence-layer-implemenation-entity-framework-core.md (dotnet#4610) Update link to the new address to avoid an extra 301 redirect. * csharp-interactive-note.md: style update (dotnet#4606) * parallel-diagnostic-tools.md: removed Visual Studio version (dotnet#4608) * adding missing language identifiers (dotnet#4611) * adding missing language identifiers (dotnet#4612) * adding missing language identifiers (dotnet#4613) * adding missing language identifiers (dotnet#4614) * adding missing language identifiers (dotnet#4615) * adding missing language identifiers (dotnet#4616) * adding missing language identifiers (dotnet#4617) * adding missing language identifiers (dotnet#4618) * adding missing language identifiers (dotnet#4619) * adding missing language identifiers (dotnet#4620) * adding missing language identifiers (dotnet#4621) * adding missing language identifiers (dotnet#4622) * adding missing language identifiers (dotnet#4623) * Fixed some attribute names (dotnet#4629) .NET puts the "Attribute" suffix in all its attribute *classes*, but not in the actual attribute. * Fix "whitespace" words - docs/standard (dotnet#4605) * base-types * fixes by pr rpetrusha * System.Reflection docs for ProcessorArchitecture is misleading (dotnet#4539) * clarify the difference betwen IA64 and AMD64 * Update ProcessorArchitecture.xml * Changed 'integer' to 'integral value' for consistency (dotnet#4586) * 👓 sweep docset for areas where `in` wasn't mentioned but should be (dotnet#4534) * sweep docset for areas where `in` wasn't mentioned but should be * respond to first round of feedback * create in-modifer article Also, update samples and snippets and make corresponding changs to 'ref' and 'out' article. * fix build errors * fix one more build error * wording change based on feedback. * respond to edits * commit changes from that unsaved window Yeah, I did that. Didn't save before committing. * added missing options (dotnet#3390) * Add warning regarding server host configuration (dotnet#4634) * Fix some xrefs (dotnet#4624) * Fix some xrefs * Fixed typo * Change hybrid xref links to normal xrefs * Remove redundant text with bad link * Update compiler-api-model.md (dotnet#4636) fix small typos * Corrected description of verbatim attribute usage (dotnet#4635) * removed sentence about migrating content (dotnet#4532) * removed sentence about migrating content * removed preview * Update the F# Guide page and some ms.date links (dotnet#4521) * Update to F# docs round 1 * yup * fix * feedback * Fix link * feedback * Updates * Updates * Updates * Make comments betteR * Feedback * Feedback * fixes * Improvements to Creating a Type Provider (dotnet#4592) * Improvements to Creating a Type Provider * Fixed assembly attribute * Revision of DateTime.Parse overloads (dotnet#4455) * Revision of DateTime.Parse * Additional modifications * Some further revisions * Additional revisions * Corrected bad XML tags * Moved examples * Additional revisions and .zip files * Added accidentally deleted file * Addressed review comments * keywords/string.md: link verbatim identifier page (dotnet#4639) * C13890: Show code for link in target versions (dotnet#4641) Hello, @BillWagner, Localization team has reported source content issue that causes localized version to have broken format compared to en-us version. Please, help to check my proposed file change into the article and help to merge if you agree with fix. If not, please, let me know either if you would like me to fix it in another way within this PR, if you prefer to fix it in another PR or if I should close this PR as by-design. In case of using another PR, please, let me know of your PR number, so we can confirm and close this PR. Many thanks in advance. * C13891: Show code for link in target versions (dotnet#4642) * Fix incorrect en dashes (dotnet#4644) * replace BNF grammar with ANTL grammar (dotnet#4643) * replace BNF grammar with ANTL grammar The BNF grammar rendering was quite messy. Antlr provides a much more clear rendering on the current engine. * fix typo and update metadata * Added output comment to verbatim string code snippet (dotnet#4646) * added missing entry (dotnet#4648) * simplify template (dotnet#4645) * simplify template * feedback * Fixed broken links to archived interop topics (dotnet#4647) * Changed HSB to HSL color model (dotnet#4652) * Changed HSB to HSL color model * Addressed review comments * Fix connect bug DevDiv # 93543 (dotnet#4653) * fix broken link (dotnet#4655) * Add displayProperty attribute to address customer complaint in SqlParameter.Value docs (dotnet#4660) * Fix connect bug DevDiv # 93543 * Add clarifications to links to address DevDiv Connect Bug # 104024 * add one more displayproperty * including the template in the treeview (dotnet#4662) Adding reference the hierarchical template in the treeview * Replace IIF with IF (dotnet#4665) * Added GetHashCode() implementation to ValueObject (dotnet#4185) This will be very helpful to readers. Picked it up from eShopOnContainers repo. * Provided correct enum members, method overloads (dotnet#4484) * Provided correct enum members, method overloads * Incorporated review comments, corrected other errors * Fixed 2 broken links * Fixed two additional links * Added exception information to SingleOrDefault (dotnet#4650) * Added information on string length >= desired string length (dotnet#4651) * Added information on string length >= desire string length * fixed broken link * Incorporated comments from @svick * TLS best practices (dotnet#4658) * IIF to IF on Tuples page (dotnet#4669) * fix: derrived --> derived typo (dotnet#4673) * Modified contributors guide to reflect contributors' project (dotnet#4666) * Add link to github issue to TLS doc (dotnet#4676) * fix link (dotnet#4672) * fix typo (dotnet#4678) * Remove extra backslash (dotnet#4680) * Fix markdown link (dotnet#4679) * Fix markdown link * feedback * update signalr info for 2.1 preview (dotnet#4681) * Fix typo (dotnet#4683) * fixed typo * fix typo * add PDF to preview serverless e-book (dotnet#4685) * Removed spaces in code (dotnet#4671) * Fixed typo in --x operator description (dotnet#4691) * Add syntax highlighting to inline code in stackalloc (dotnet#4690) Just noticed on docs.microsoft.com that these code blocks don't have syntax highlighting, which does not adhere to the style guide * Add UWP as platform that supports .NET Standard (dotnet#3813) * Add UWP as platform that supports .NET Standard https://blogs.msdn.microsoft.com/dotnet/2017/10/10/announcing-uwp-support-for-net-standard-2-0/ * Add platforms; remove versions; add link Instead of listing the same platforms that are listed on https://docs.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support and having to maintain both lists as new versions are released, just mention the platforms here and link to the existing table with versions. Closes dotnet#3814 * feedback * < and > wrong (dotnet#4664) * < and > wrong < and > were not in code * Updated xref Updated xref to correct signature * removing tickmarks and overloads * some other minor fixes * Update net-standard.md (dotnet#4694) * Correct time complexity of List<T>.InsertRange (dotnet#4693) * Operator new doesn't create structs on the stack (dotnet#4667) * Operator new doesn't create structs on the stack * Small improvements to operator new * Structs can contain resources too * Mention garbage collector * Clarified when value types are destroyed
Contributes to #4561.
cc: @BillWagner