Home > Backend Development > C++ > Heap or Stack: Where Does `new` Allocate Structs in C#?

Heap or Stack: Where Does `new` Allocate Structs in C#?

Patricia Arquette
Release: 2025-01-13 15:06:46
Original
787 people have browsed it

Heap or Stack: Where Does `new` Allocate Structs in C#?

Memory allocation of structure by new operator in C#: heap or stack?

When a class is instantiated using the new operator, memory is allocated on the heap. However, the behavior of the new operator on structures depends on the specific scenario. Let’s explore the differences:

Constructor with parameters

When calling the parameterized constructor of a structure using new, memory is allocated on the stack. This is similar to assigning a value to a local variable of a value type.

<code class="language-csharp">Guid local = new Guid("");</code>
Copy after login

The assigned IL code uses newobj to allocate memory on the stack and initialize the value using the provided string.

Constructor without parameters

When calling the parameterless constructor of a structure using new, the behavior depends on the context:

  • is assigned to a field or local variable:

No memory will be allocated on the stack. Instead use initobj to initialize an existing storage location (field or local variable). Value types are constructed in-place.

<code class="language-csharp">Guid field;
...
field = new Guid();</code>
Copy after login
  • Intermediate value of method call:

Allocate a temporary local variable on the stack and initialize it using initobj. This value is then passed as a parameter to the method.

<code class="language-csharp">MethodTakingGuid(new Guid());</code>
Copy after login
  • is assigned to instance variables or static variables:

No memory will be allocated on the stack. Value types are constructed directly at the storage location of the instance or static variable.

<code class="language-csharp">myInstance.GuidProperty = new Guid();</code>
Copy after login

Conclusion

The assignment behavior of the

new operator when used with a structure depends on the context. For constructors with parameters, memory is always allocated on the stack. For parameterless constructors, memory may not be allocated on the stack, depending on the context. This behavior depends heavily on the IL directives generated by the compiler when converting C# code.

The above is the detailed content of Heap or Stack: Where Does `new` Allocate Structs in C#?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template