V
V
Vadim Nikiforov2022-01-24 23:28:20
.NET
Vadim Nikiforov, 2022-01-24 23:28:20

What is the difference between value and reference types? Why did they come up with such a division? It was impossible to come up with only either significant or reference?

What is the difference between value and reference types?

Value types are stored on the stack. A stack is a data structure that grows from the bottom up: each new element is placed on top of the previous one. The lifetime of variables of these types is limited by their context. Physically, the stack is some area of ​​memory in the address space. And reference types are stored on the heap, this is another area of ​​memory that can be thought of as an unordered collection of different objects. When an object of a reference type is created, a reference to the address on the heap is placed on the stack. When this object is no longer used, the reference is destroyed and the memory on the heap is cleared.
Maybe someone has something to add?

Why did they come up with such a division?

It was impossible to come up with only either significant or reference?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
@
@insighter, 2022-01-25
@nikifovadim

It is impossible to store everything on the stack for ordinary programs.
It is realistic to store everything on the heap (it is enough to pack all the valued variables), but it is very, very not optimal. Working with the stack is much faster, there are specialized processor registers to work with it, and in order to access a section of arbitrary computer memory, even at a low level, you need to do a lot of operations. And the stack is cleared instantly, no garbage collectors are needed.

Y
Yuri Shporkhun, 2022-01-28
@xJAYx

Everything that you described on value types is almost all wrong. And it doesn’t matter where everything is stored, 99% of programmers still don’t use this knowledge - they just show off in front of each other. The most important thing there is the semantics of copying ... that's all ... All experts on storing structures immediately merge on the question of cache-aware and cache-oblivious algorithms (where this could really come in handy!)
Value types (value type) are stored in stack. - no, that's not true. They can be stored on the stack. Even local value types may not be stored there.
public void m1() {
int i = 0;
}
Even this variable can be stored on the heap if someone adds a bottom closure.

M
Myclass, 2022-01-25
@Myclass

the answer lies in the very essence. Save a byte, int, double word or address variable - all you need for this is just a byte, two bytes, or as much as it is relevant today - for example, 8 bytes (64 Bit).
It's always more or less easy to organize. For example the same int array with its elements. They are all right next to each other. If we are talking about complex structures, starting with string, then it is already easier to save and organize addresses for those places where these structures actually lie.
And that's it - there is no malicious intent or stupidity. Everything is done for the simplicity of organizing all these things and the ease of coping with tasks such as creating variables / objects, modifying them and deleting them.
Same as with the Carbadge Collection. Spending time on the "exact" removal of objects from memory while the program is running is not to respect your program. But here is the distribution of actions, and in your question the distribution of places.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question