Note: if after reading this question you think, "how can that even happen", that is ok. If you want to keep an open mind, there are some points after the question that you can follow and that show how this can happen and why this is useful. Just remember that this is just a question and not a tutorial on any of these topics. The comments have enough noise already and they are hard to follow. If you have questions about these topics, I would appreciate if you post them as questions in SO instead of in the comments.

Question: If I have an object of type int stored at the address pointed by c

int* c = /* allocate int (returns unique address) */; *c = 3;

referred by two pointers a and b :

int* a = /* create pointer to (*c) */; int* b = /* create pointer to (*c) */;

such that:

assert(a != b); // the pointers point to a different address assert(*b == 3); *a = 2; assert(*b == 2); // but they refer to the same value

Is this undefined behavior? If yes, which part of the C++ standard disallows this? If not, which parts of the C++ standard allows this?

Note: the memory c points to is allocated with a memory allocation function that returns an unique address ( new , malloc , ...). The way to create these pointers with different values is very platform specific, although in most unix systems it can be done with mmap and on windows it can be done with VirtualAlloc .

Background: most operating systems (those that have a userspace that is not on ring 0) run their processes on virtual memory, and have a map from virtual memory pages to physical memory pages. Some of these systems (Linux/MacOS/BSDs/Unixes and 64bit windows) provide some system calls (like mmap or VirtualAlloc ) that can be used to map two virtual memory pages to the same physical memory page. When a process performs this, it can essentially access the same page of physical memory from two different virtual memory addresses. That is, those two pointers will have a different value, but they will access the same physical memory storage. Keywords to google for: mmap , virtual memory, memory pages. Data-structures that use this feature for profit are "magic ring buffer"s (that's the technical term), and non-reallocating dynamically-sized vectors (that is, vectors that do not need to reallocate memory when they grow). Google provides more information about these than I could ever fit here.

Very minimal probably non-working example (unix only):

We first allocate an int on the heap. The following request an anonymous, non-file-backed, mapping of virtual memory. One must request here at least a whole memory page, but for simplicity I'll just request the size of an int ( mmap will allocate a whole memory page anyways):

int* c= mmap(NULL, sizeof(int), PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE,-1, 0);

Now we need to map this to two independent memory locations, so we map it to the same memory-mapped file, twice, to, e.g., two adjacent memory locations. We won't really use this file, but we still need to create it and open it:

mmap(c, sizeof(int), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, some_fd, 0); mmap(c + 1, sizeof(int), PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED, some_fd, 0);

Now we are almost done:

int* a = c; int* b = c + 1;

These are obviously different virtual addresses:

assert(a != b);

But they point to the same, non-file-backed, physical memory page:

*a = 314; assert(*b == 314);