Perkel
Arcane
- Joined
- Mar 28, 2014
- Messages
- 15,869
Slight correction: x is a different pointer than y. But both pointers point to the same memory. In higher level languages they are called references. The difference being in that with pointers you can manipulate what memory they point to directly while with references you cannot.
And there is also a but. Some things are not references - primitive types. So for example if you do in python:
This also means if you pass an int to a function it will be copied.Code:x = 3 # x is int with value 3 y = x # y is an int with value 3 y = 4 # y is an int with value 4 print x # prints 3
That's why I mentioned in one post you should look into pass by reference and pass by value.
I don't get above.
If x and y now have same address space then why x didn't change to 4 ?
I can't fallow how it would look like in assembler:
x is defined and it takes adress in memory let's say adress "a"
then we type function so pointerx ------ > 00000003 to adress "a" Adress a now is 00000003
y is defined it takes adress in memory let's say "b"
but y pointer changes to x pointer so changes in y will lead to change 00000003 to something else
then we change 00000003 through pointer y to 00000004
we print out memory space connected to x and that should give me hex ending with 3 not 4.
Almost right but the details are not important.
If you have unbearable curiosity, look up the difference between stack and heap, why they exist and how primitives get allocated to each, and why and how that interacts with registers.
thanks i will look for that in spare time. It is interesting imo.
guess. In case like you mentioned above it does some additional math fallowing some logic that doesn't fallow normal way. (if i can call something "normal")