Question: I came across some code when reading about how memory is allocated in C++. But I could not figure out why two ValueHolder pointers (x, y) point to the same address. What does the code really mean?

```
struct ValueHolder{
  int value;
  operator ValueHolder* () { return this; } // just typecast 
};

void objects_stored_by_pointers_with_autocleanup() {
  ValueHolder * x = ValueHolder();
  ValueHolder * y = ValueHolder();
  x->value = 7;
  y->value = 9;
  printf("%d\n", x->value);
  printf("%d\n", y->value);
  printf("%p\n", (ValueHolder*)x);
  printf("%p\n", (ValueHolder*)y);
}   
int main(int argc, char* argv[]){
  objects_stored_by_pointers_with_autocleanup();
}

```

Expected Result:

```
7
9
0x55afde2aae70
0x55afde2aae90

```

Actual result:

```
9
9
0x7ffeb6d8fcf4
0x7ffeb6d8fcf4

```

Answer: 
> 
> What does the code really mean?
> 
> 
> 

`ValueHolder()` creates a temporary object, after the full expression it's been destroyed, the memory occupied is released too. After that, the pointer `x` and `y` become dangled immediately, any dereference on them like `x->value` and `y->value` leads to UB.

It seems for your case the 2nd temporary object gets the same address as the 1st one which has been destroyed, this is fine, but note it's not guaranteed.
What you're seeing is due to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior).

The expression `ValueHolder()` creates a *temporary* object, which is immediately destructed. The pointer returned by the conversion operator will be invalid as soon as the initialization is done.

Because the objects are temporary, it allows the compiler to reuse the same memory for the next time you create a temporary object.