简体   繁体   中英

Why do pointer / variable memory addresses not change?

#include <iostream>
using namespace std;

int main(void)
{
    int *ptr = new int;
    cout << "Memory address of ptr:" << ptr << endl;
    cin.get();
    delete ptr;

    return 0;
}

Every time I run this program, I get the same memory address for ptr . Why?

[Note: my answer assumes you're working with a modern OS that uses a virtual memory system.]

Due to virtual memory, each process operates in its own unique address space, which is independent of and unaffected by any other process. The address you get from new is a virtual address , and is generated by whatever your compiler's implementation of new chooses to do. * There's no reason this couldn't be deterministic.

On the other hand, the physical address associated with your virtual memory address will most likely be different every time, and will be affected by all sorts of things. This mapping is controlled by the OS.


* new is probably implemented in terms of malloc .

I'd say it's mostly coincidence. As the memory allocator/OS can give you whatever address it wants.

The addresses you get are obviously not uniformly random (and is highly dependent on other OS factors), so it's often to get the same (virtual) address several times in the row.

So for example, on my machine: Window 7, compiled with VS2010, I get different addresses with different runs:

00134C40
00124C40
00214C40
00034C40
00144C40
001B4C40

This is an artifact of your environment. The cin.get() suggests to me that you are compiling and executing in Visual Studio, which provides an unusually predictable runtime environment. When I compile and run that code on my linux, two executions gave two different addresses.

ETA:
In comments you expressed an expectation that different processes could obtain the same memory address and that this address would be inaccessible to your program. In any modern operating system this is not the case, because the operating system is providing each process with virtual memory address spaces.

Only the operating system sees the true hardware addresses, and maintains virtual memory maps for each program, redirecting virtual addresses to physical addresses. Therefore, an arbitrary number of different processes can hold data in the same virtual address, while the operating system maps that address to a separate physical address for each process.

This guarantees that process A cannot read or write to memory in use by process B without a special provision enabling such access (such as by instructing the OS to map certain virtual memory in certain processes to the same physical memory). It allows the operating system to make different kinds of memory hardware transparent to programs.

It also allows the OS to move a program's data around behind its back to optimize system performance. Classical example: Moving data that hasn't been used for some time to a special file on the hard disk. This is sometimes called the page file .

Memory maps are typically broken up into pages : Blocks of contiguous memory of a certain size (the page size ). Data held within a page of virtual address space is usually also contiguous in physical memory, but if data runs over a page boundary, information that appears contiguous in virtual memory could easily be separated. If a C/C++ program enters undefined behavior, it may attempt to access memory in a page that the OS has not mapped to physical memory. This will cause the OS to generate an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM