Memory address values outputted by C++ 14 compiler on a Mac Mini with 16 GB of RAM (+1 GB virtual) show just shy of 128 TB range, even the difference between the stack and heap is over 186 GB.
I had just installed the latest C++ compiler (to refresh my C++ knowledge) on my Mac Mini with Intel CPU, 16 GB of RAM, 1TB SSD.
#include <iostream>
int main () {
double value = 2.0;
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
*pvalue = 29494.99; // Store value at allocated address
std::cout << "Value of value : " << value << std::endl; // This is stored on stack
std::cout << "Address of value : " << &value << std::endl; // 0x7ffedfd08a30 (just below 128 TB)
std::cout << "Value at pvalue : " << *pvalue << std::endl; // This is stored on heap
std::cout << "Value of pvalue : " << pvalue << std::endl; // 0x7fd05f402830 (just below 128 TB)
// Even the difference is: 0x2E80906200 (over 186 GB)
delete pvalue; // free up the memory.
return 0;
}
/usr/local/bin/c++-9 heap.cpp -o heap
./heap
I was curious to see the memory ranges of the stack and heap. I expected both numbers to be within my system's total physical memory range: 16 GB RAM, 1 GB Virtual RAM, 1 TB SSD RAM . However, the results for both Stack and Heap show just shy of 128 TB, and even the difference between them is more than 186 GB:
Value of value : 2
Address of value : 0x7ffee1776a30
Value at pvalue : 29495
Value of pvalue : 0x7fd05f402830
I had researched this oddity with no good result. Anybody can explain this please?