Denis Yurin
Denis Yurin

Reputation: 71

Pointer value exceeds available memory? How?

I work on Peano-Hilbert data ordering (c++ 4.9, linux 64x) to coalesce dynamically allocated memory. For control I am trying to visualize the actual data distribution in the memory. For this I convert pointers to my data to integers like follows

unsigned long int address =  *(unsigned long int*)(&pointer);

and then plot them as some 2D-map. It works fine for most of the case but sometimes I get values exceeding by far available memory, e.g. 140170747903888, which corresponds ~127 TB shift whereas I have only 16 GB of RAM. What the hell?

Upvotes: 2

Views: 163

Answers (2)

Tony Delroy
Tony Delroy

Reputation: 106236

AMD 64 bit uses 48 bits for virtual memory addresses, which corresponds to 256TB. Virtual address space is distinct from physical RAM: the addresses are looked up in a table on the CPU and actual RAM faulted in when the pages in question are first accessed.

Upvotes: 1

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31193

The memory management system does not handle memory in a linear way. It is free to tell a process that some memory block is in the address 0x1234123412345678, even if you only had 128MB of memory. This is called paging. The data might not even be in physical memory, but pages out to disk.

This means that you have no way of knowing where in physical memory anything is from the pointer value, since it might change all the time (or it might not even be in memory), you only know the virtual address the OS has happened to give you. And it is totally implementation dependent how it gives them out.

Upvotes: 2

Related Questions