Images will help.
Automatic storage (stack) Dynamic storage (heap) ------------------------- ---------------------- Item Address Value Address Value ---- ------- ----- ------- ----- nights 0xff001000 1001 pt 0xff001004 0x00b0fff0 ------> 0x00b0fff0 1001 pd 0xff00100c 0x00b0fff4 ------> 0x00b0fff4 10000001.0
nights , pt and pd objects have auto storage duration. In most implementations, this means that they are allocated from the runtime stack. The nights object lives at 0xff001000 1 and stores the value 1001 . The pt object lives at 0xff0010004 and stores the address of the dynamic object created by new , which is 0x00b0fff0 . The pd object lives at 0xff00100c and stores the address of another dynamic object created by new , which is 0x00b0fff4 .
Heap objects at addresses 0x00b0fff0 and 0x00b0fff4 store the values 1001 and 10000001.0 respectively.
Edit
FWIW, I wrote a dumper utility some time ago that uploads the addresses and contents of objects; with code
#include <cstdio> #include "dumper.h" using namespace std; int main( void ) { int nights = 1001; int *pt = new int; *pt = 1001; double *pd = new double; *pd = 1000001.0; char *names[] = { "nights", "pt", "pd", "*pt", "*pd" }; void *addrs[] = { &nights, &pt, &pd, pt, pd }; size_t sizes[] = { sizeof nights, sizeof pt, sizeof pd, sizeof *pt, sizeof *pd }; dumper( names, addrs, sizes, 5, stdout ); return 0; }
I get a conclusion
Item Address 00 01 02 03 ---- ------- -- -- -- -- nights 0x7fff9efe7c6c e9 03 00 00 .... pt 0x7fff9efe7c60 10 20 50 00 ..P. 0x7fff9efe7c64 00 00 00 00 .... pd 0x7fff9efe7c58 30 20 50 00 0.P. 0x7fff9efe7c5c 00 00 00 00 .... *pt 0x502010 e9 03 00 00 .... *pd 0x502030 00 00 00 00 .... 0x502034 82 84 2e 41 ...A
In this case, the addresses are valid. On my system (x86_64 / Linux SLES-10), stack addresses start high and grow “down” (towards lower addresses), and heap addresses start from a low level and grow “up” (towards higher addresses).
x86 is unimportant, that is, the address byte is the least significant byte; multibyte objects should be read from right to left.
- All addresses are composed of air and are not intended to represent a real implementation or real world architecture.