Discussion on the value type pointing difference between PHP and C

  • 2020-06-01 09:19:24
  • OfStack

The difference between the value copy of PH and C# (point out if you're wrong!)
$a = 2;
$b = $a; // in php we're pointing the address of b to a so b is also equal to 2; That's the difference
$a = 5; // at this point, the value of a in php is rewritten, so the php core will then reassign b to one address, and then copy the original value of a. This is the copy on write principle, that is, unless a write is performed, the value type points to an address.
But C #. Copy of a value type. Always create a new address:
int a = 2;
int b = a; // whether or not a was written twice. .NET will allocate 1 new memory space to b (the value is stored in the stack space). Then make a copy of the a value
Note: values of the C# median type are stored directly in the stack. The reference type, the reference address is stored in the stack, and the actual value is stored in the heap. According to the address of the stack, find the value in the heap.

Related articles: