Question

As we all know, stack address are higher than heap addresses in Process Address Space. But when I wrote a program to verify it in VS2010, I got some trouble.The stack has an address lower than heap, even lower than that of Data Segment. the program is shown as follows:

#include "stdafx.h"
#include "malloc.h"
static int g_a=123;
int g_b=123;
int main()
{
    static int a=123;
    int b=123;
    float c[10]={0};
    int *p1=(int*)malloc(sizeof(int));
    int *p2=(int *)malloc(5*sizeof(int));
}

Here are their address according to the VS2010:

&g_a    0x01097038 
&g_b    0x0109703c
&a      0x01097040

&b      0x002af7a8
c       0x002af778

p1      0x00571500
p2      0x00571540

Obviously, the pointer p1 , which points to an array on the heap, has a bigger address than &b, which is on the stack. That's why?

PS:sorry for the absence of picture due to by my poor reputation, or I could describe my question more clearly.

Was it helpful?

Solution

"As we all know, stack address are higher than heap addresses in Process Address Space."

Your assumption here is false. The stack and heap are both allocated from the process's virtual address space and they may, for all intents and purposes, be located practically anywhere in that address space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top