How do you store int or other types of C # values ​​in a heap (with C #)?

I am educating about C # through the Troelsen Pro C # book.

I am familiar with the stack and the heap, and how C # stores such things. In C ++, whenever we use new , we get a pointer to something on the heap. However, in C #, the new behavior seems different to me:

  • when used with int value types using new seems to just call the default int constructor, but the value of such an int will still be stored on the stack

I understand that all objects / structures, etc. stored on the heap regardless of whether new .

So my question is: how can I create an int instance on the heap? (And does this have anything to do with boxing?)

+4
source share
6 answers

You can enter any type of value into the System.Object type so that it is stored in a managed heap:

 int number = 1; object locatedOnTheHeap = number; 

Another question: why do you need it.

This is a classic example from the required MSDN document: Boxing and Unboxing (C # Programming Guide)

When the CLR enters a value type, it wraps the value inside System.Object and stores it on the managed heap. Boxing is used to store types of valuables in a garbage collected heap. Boxing is an implicit conversion of a value type to an object of a type or to any type of interface implemented by this type of value. The value box of type allocates an instance of the object on the heap and copies the value to the new object.

.

I understand that all objects / structures, etc. stored in a heap

BTW, IIRC sometimes JIT optimizes the code, so objects of type value type type int are stored in the CPU register, and not on the stack.

+3
source

I don’t know why you would like to do this, but in theory you really could use your value. You would do this by inserting an int into the object (which is a reference type and will be pushed onto the stack:

 object IAmARefSoIWillBeOnHeap = (object)1; 

* As indicated in sll, you do not need (object) , since it will be implicit. It is here for academic reasons only to show what is happening.

Here is a good article on reference and type values , which is the difference between a heap and a stack

+2
source

but the value of such an int will still be stored on the stack

This is not necessarily the case. Even when it’s true, it’s a pure detail of the implementation, not part of the language specification. The main problem is that the type system does not necessarily correlate with the storage engine used by the runtime.

There are many cases where calling new in a structure still causes the object to not be on the stack. Boxing is a good example - when you place an object, you basically click on the object (effectively "copying" it into the heap) and referring to the object. Also, anytime you close a value type with a lambda, you end up "allocating a bunch."

If I say that I would not focus on this at all, the problem really should not concern the stack and heap in the distribution, but rather about the types of values ​​and the semantics of the reference type. Therefore, I highly recommend reading Eric Lippert's “Truth About Value Types” and “John Skeet” References and Values . Both of these articles focus on important aspects of struct vs. semantics. class instead of having to view the repository.

Regarding ways to force int to be stored on the heap, here are a few simple ones:

 object one = 1; // Boxing int two = 2; // Gets closed over, so ends up "on the heap" Action closeOverTwo = () => { Console.WriteLine(two); } // Do stuff with two here... var three = new { Three = 3 }; // Wrap in a value type... 
+2
source

The value type is "allocated" wherever it is declared:

  • As a local variable, usually on the stack (but in order to rephrase Eric Lippert, the stack is an implementation detail, I suggest you read its wonderful blog post: The truth about value types .)
  • As a field in the class, it extends the size of the instance with the size of the value type and occupies a space inside the instance

So this code:

 var x = new SomeValueType(); 

Don't allocate something on the heap by itself for this type of value. If you close it with an anonymous method or similar, the local variable will be converted to a class field, and an instance of this class will be allocated on the heap, but in this case the value type will be built into this class as a field.

The heap is for instances of reference types.

However, you have touched on something about boxing. You can enter a value of type value to make a copy of it, and put that copy in a heap wrapped in an object.

So this is:

 object x = new SomeValueType(); 

first select the type of value, then paste it into the object and save the reference to this object in x .

+2
source

If you want to inject int into a heap, you can do this:

 object o = 4; 

But basically, you should not want this. C # is for you not to think about such things. Here's a good place to start: http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx

+1
source

So my question is: how can I create an int instance on the heap? (And does it have something to do with boxing?)

Your understanding of objects and structures is correct. When you analyze an object or structure, it goes into a heap.

0
source

Source: https://habr.com/ru/post/1403713/


All Articles