u/Additional-Start661

▲ 13 r/cpp_questions+1 crossposts

How memory address is created in c++ while using the pointer ?

int *ptr1 = new int(10);

int *ptr2 = ptr1;

delete ptr2;
I understand that the heap memory is freed, and ptr1 becomes a dangling pointer because it still stores the old address.

But I’m confused about the address itself.

My questions are:

  1. Is the memory address itself created inside the heap?
  2. When we calldelete, is the address removed or only the data at that address?
  3. After memory is freed, can the same address later be reused for another variable?
  4. Where is the pointer variable stored, vs where is the actual heap memory stored?

I’m trying to understand the difference between:

  • the pointer variable,
  • the address value,
  • and the actual heap memory block.”
reddit.com
u/Additional-Start661 — 8 days ago