0

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

tadman
  • 208,517
  • 23
  • 234
  • 262
Mohammad Ansari
  • 1,076
  • 1
  • 11
  • 22
  • 1
    I doubt that RamMap would be able to resolve anything beyond the memory that OS has allocated for your program. – ryyker Jun 17 '20 at 19:28
  • @bhristov But why it does not show in memory list? – Mohammad Ansari Jun 17 '20 at 19:28
  • @ryyker How i can see all the memory ,Any program? – Mohammad Ansari Jun 17 '20 at 19:28
  • 2
    If that is important, then you can map each variable by outputting its address, i.e. for example `int array[10] = {1,2,3,4,5,6,7,8,9,0};` then `printf( memory: %p, %p, ..., %p\n", array[0], array[1], ..., arraay[9]);` – ryyker Jun 17 '20 at 19:32
  • @ryyker No i just want to understand the way this stuff works – Mohammad Ansari Jun 17 '20 at 19:33
  • 2
    @MohammadAnsari See for example [Virtual Memory or Physical Memory](https://stackoverflow.com/questions/24480899/virtual-memory-or-physical-memory). The physical memory address can change while your program is running, even though the virtual address stays (of course) the same during the lifetime of the process. – dxiv Jun 17 '20 at 19:34
  • 1
    @ryyker Did you mean `printf( memory: %p, %p, ..., %p\n", &array[0], &array[1], ..., &array[9]);`? Added `&` – chux - Reinstate Monica Jun 17 '20 at 19:34
  • 1
    @dxiv Thanks for your comment , i think this is my needed answer , it is not actual piece of memory ,it's virtual memory – Mohammad Ansari Jun 17 '20 at 19:36
  • 1
    @chux-ReinstateMonica - Ooops, yes thank you. That's what happens with my attention is divided between `printf` statements and carrot cake. :) – ryyker Jun 17 '20 at 19:37
  • Talk about divided attention -- wait until you see the latest on Meta [Reachtions](https://meta.stackoverflow.com/q/398367/3422102) Oh brother... – David C. Rankin Jun 17 '20 at 21:42

1 Answers1

4

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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 2
    With [ASLR](https://en.wikipedia.org/wiki/Address_space_layout_randomization) the chances of it mapping 1:1 with the physical address are basically zero. – tadman Jun 17 '20 at 21:27