Mohammad Ansari
Mohammad Ansari

Reputation: 1549

Physical memory address and pointer address is not same

Hi i'm playing with c++ pointers and also using program called RamMap from microsoft for inspecting memory physical address.

but i figure out that the address of variable pointer in c++ does not exist in list of RamMap

For example:

#include <cstdlib>
#include <iostream>
#include<conio.h>
using namespace std;

int main()
{
    string var1="var1";
    string * foo = &var1;
    cout<<foo;

    getch();
    return 0;
}

It return 0x61fde0 enter image description here

After that i looked at RamMap and search this address but can't find anything

enter image description here

Can anyone help me? i'm so confused and i noticed my pointer address does not change every time i rerun program

Upvotes: 0

Views: 451

Answers (1)

David Schwartz
David Schwartz

Reputation: 182827

Every process has its own view of memory that consists of logical addresses. They may or may not correspond to physical addresses, that is, actual RAM. But even when they do correspond, the values are still different.

If this wasn't the case, a lot of things would go wrong. But the most obvious one is probably the issue of physical memory fragmentation. Imagine if a process requests to allocate a 4MB block of memory and there's plenty of RAM free, but no contiguous 4MB chunk. Ouch.

Upvotes: 4

Related Questions