Consider
void d(int* t) { //pointer passed by value
delete t;
std::cout << *t << '\n';
}
and
void d2(int*& t) { //pointer passed by reference
delete t;
std::cout << *t << '\n';
}
Say we have:
int *y = new int{22};
d(y);
d2(y);
- In the first case (d(y)), my understanding is a copy of a pointer is created.
- In the second case, my understanding is I passed it by reference, so still one pointer points to y
In both cases, I expected:
std::cout << *t << '\n';
to cause undefined behavior because I should have deleted the value of t. But I can still dereference it successfully, as if "delete t;" did nothing. Why is that?