Why does pointing to the same memory address change an object?

Asked

Viewed 273 times

6

If I run the following code:

let a = [1, 2, 3]
let b = a

In this case, the variable 'a' and 'b' are pointing to the same memory address, consequently, everything I change to 'a' will theoretically be changed to 'b'. For example:

a[0] = 4
console.log(b)
> [4, 2, 3]

So far, so good. But if I do any of these things that I will attach below, it seems that automatically 'a' and 'b' fail to point to the same address. For example:

a = [1, 2, 3]
console.log(b)
> [4, 2, 3]

or

a = {nome: 'Gustavo', sobrenome: 'Paiva'}
console.log(b)
> [4, 2, 3]

or

a = 23
console.log(b)
> [4, 2, 3]

Why does this happen?

After these things, I feel I don’t quite understand the concept of 'pointing to the same memory address'.

2 answers

6


Actually this idea of pointing is slightly wrong. There is even that, but let’s start at the end that you understand better.

When you assign 23 to the variable a is very easy to understand. You are placing a numeric object whose value is 23 in the storage location with a name called a.

If you don’t understand that you need to read What is a variable?.

And if you put the number 12 in a?

Is putting a another numeric object whose value is 12. Noted the highlight?

I think we can understand that the previous value is discarded.

You can do it all you want.

And in Javascript you can put objects of other types because it’s a dynamic typing language, so only objects have types, the variable does not.

What happens if you do as at the beginning and now assign a array for a? What will be stored in a?

Types by value X type by reference

What I showed initially is the type by value that is easier to understand. The object goes straight into the variable.

It turns out that there are types by reference. They are types that depend on indirect. So you have a reference which is a pointer to another object and in another different place has the actual object.

This is necessary because this object has a size to be defined, there is no way to reserve memory previously in the variable’s storage location. Even if I knew how big it could be.

What is the value that goes in the variable? The value of the pointer, which is a memory address to where the object actually is, then enters the pointer.

When a is worth 23 and you do:

b = a

you are copying the numeric object that has the value 23 that is in a to the site of b. You will have two variables as the same value, although they are different objects, physically there are two. Simple.

But what you’re copying when you have an object is by reference to array?

It’s copying the reference, the pointer. Then you have two variables with the same value, a memory address pointing to the same object. This you can see. When you changed the composition of the object by one variable the object changed by the other because it is the same object. Two pointers but only one object.

But what happened when you did this?

a = [1, 2, 3] //isto é um literal de criação de array

You just created an object new and assigns to a the address to her. And what happened to b? Kept pointing at the object he was pointing at before, no one touched b.

So now you have a pointing to an object that has just been created and b pointing to another object that already existed before. No longer has two variables pointing to the same object.

Noted that the value that was in a what was the reference for the first object was discarded? It happened equal to the type by value 23, it has been discarded. Not necessarily the pointed object. In fact the object still exists because b continues pointing to it. If no one pointed to this object but it would be discarded as well. This is the type mechanism by reference.

If the goal was to understand memory management of how this happens the question would be another.

Completion

So that’s it, in types by value you always create new objects when you assign something to the variable, in types by reference only in some situations it happens, only creates a new object take off create a new object. Assign b = a in a reference type is not creating a new object, only when you use a syntax that is clearly a new object literal is you creating something new.

  • Thank you very much, I couldn’t have given you a better explanation!

  • 1

    Related/complementary: https://answall.com/questions/2695/howto function

2

When you use the let (or var) you give a value to that variable. When you assign a new value to that variable it loses relation to its previous values. That is:

let a = 2;
a = 3; // dá 3

Similarly the variable loses connection to that pointer, which in your example continues to exist in b.

It’s no more complicated than that: assign a new value to a variable "cut" the connection to its previous value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.