Same Object Different Address. Why

Why addresses of two different objects should be different?

Having two objects at the same address would mean that there would be no way to distinguish between these two objects when referencing them with pointers. For example, in the following code:

EmptyClass o1;
EmptyClass o2;

EmptyClass * po = &o;
po->foo();

Should the foo method be called on o1 or o2?

It could be argued that since these objects have no data and no virtual methods (otherwise they would have a non-zero size), it doesn't matter on which instance the method is invoked. However, this becomes more important when we want to test if two objects are equal (i.e. if they are the same):

template < typename T >
bool isSame( T const & t1, T const & t2 )
{
return &t1 == &t2;
}

EmptyClass o1; // one object and...
EmptyClass o2; // ...a distinct object...

assert( ! isSame( o1, o2 ) ); // ...should not be one and same object!

For a more concrete example, let's assume that I want to associate some objects (I could say entities) with some values, let's say in an associative container:

Person you;
Person me;

// You and I are two different persons
// (unless I have some dissociative identity disorder!)
// Person is a class with entity semantics (there is only one 'me', I can't make
// a copy of myself like I would do with integers or strings)

std::map< Person *, std::string > personToName;

personToName[&you] = "Andrew_Lvov";
personToName[&me] = "Luc Touraille";
// Oh, bother! The program confused us to be the same person, so now you and I
// have the same name!

So yes, it all boils down to object identity: if objects were allowed to be empty, they could be deprived of their identity, which is simply not allowed by the language (thankfully).

Are object variables with the same address always identical in JavaScript?

Two objects cannot occupy the same location, either in our physical universe or in the twisted javascript universe. What you can have are two objects whose values are references to the same memory location.

var x = [1, 2, 3]
var y = x

So, the array [1, 2, 3] might be stored in some memory address, let's say it starts at 00A4. The variable x will be stored in some different place in memory, let's say 0108, and the actual value stored in that location is the number 00A4, that represents the memory location of its referenced value. Now, y will be then store in another totally different memory location, let's say 020F, and its value will also be 00A4, the memory location of its referenced value.

  1. Both x and y are variables whose values are references, that ultimately point to the same address. Reference values that point to the same address are always equal when compared.

  2. Javascript doesn't have any low-level semantics like this to manipulate memory addresses. You could do that maybe by using c procedure calls or something.

Why does the object created without using new operator gets the same address

You keep creating the object on the top of the stack, and then removing it (the object goes out of scope at the end of each loop iteration, and is deconstructed before the next iteration). Thus, the address of the top of the stack is the same everytime you allocate space for the object.

If you remove the loop and allocate multiple Nodes in a row, you'll see different addresses for each as the stack grows.

Why do the objects created in a loop have the same address?

This stuff is explained in every college-level course on operating system design and/or compiler design.

The objects in question are instantiated on the stack, and at the end of the scope, they go out of scope on the stack. The stack grows and shrinks accordingly, and because on every iteration of the loop, the same exact objects get created (and destroyed), the stack grows and shrinks by the same amount, so the same objects always end up getting instantiated at the same location on the stack.

In practice, the compiler can do some optimizations and figure out the large amount of stack each function will use, most of the time, and thus "pregrow" the stack to the large amount the function will need; but that's going a bit too far into the weeds...

This is as concise answer as I can give, without going into an entire lecture of what a stack is, etc...

Now, the other object is allocated on the heap. Focus your attention on the fact that, at the end of the loop, the object you stuffed into the vector still exists, while the "a" object gets destroyed, and you'll begin to see the difference. On each iteration of the loop the "a" object gets created, and then destroyed at the end of the loop (and get created at the beginning of the loop again), while the object you stuffed into the vector still exists.

Different memory address for same object

  • your x inside class __init__ should give error. Your code is running because you are using IDE like syder/jupyter which store previously run code's result. If you restart your IDE and run same code again it will raise error name 'x' is not defined
  • If you want to reference x (object of class) use self to get that reference.
>>> class test:
... def __init__(self, max):
... print(self, "b")
...
>>> x = test(2)
<__main__.test object at 0x0000016A080052C8> b
>>> print(x,"a")
<__main__.test object at 0x0000016A080052C8> a
  • as you can see both x and self inside __init__ have same address.
  • Some reference to look at : What __init__ and self do on Python?


Related Topics



Leave a reply



Submit