Why heap address is bigger than stack address in Visual Studio debugger?

  c++, heap, stack

My test code is as below.

int* a;
int b = 20;
int c[5] = { 1,2,3,4,5 };
int* d = new int[5]{ 1,2,3,4,5 };
int* e = new int[5]{ 1,2,3,4,5 };
int* f = new int[5]{ 1,2,3,4,5 };

a = new int(10);

cout << a << endl;
delete a;
delete[]d;
delete[]e;
delete[]f;

My picture is as below.

enter image description here

As you can see, local varaibles like a, b, c .. f have addresses that decrease.
So, I thought that these variables would have higher addresses than addresses of allocated variables (on heap) because local variables are allocated on stack and stack has higher address than heap (at least, I have learned that in OS testbook).

However, it is not.. as shown in picture, value ‘d'(heap address) is bigger than ‘&d'(stack address).
and I have read two materials below.

Why is the stack address lower than that of heap in Visual C++?

Does visual studio show virtual or physical address while debugging?

These show that visual studio debugger always uses virtual addresses, and the statement "As we all know, stack address are higher than heap addresses in Process Address Space." can be false.

However, I am still in confuse.. because my test shows that local variables’ addresses decrease when allocated.

Is it just a coincidence that local variables’ addresses decrease and heap addresses are bigger than stack addresses?

I am looking forward your precise and nice answers.

Thank you for reading.

Source: Windows Questions C++

LEAVE A COMMENT