为什么在Visual C ++中堆栈地址比堆地址低? [英] Why is the stack address lower than that of heap in Visual C++?

查看:154
本文介绍了为什么在Visual C ++中堆栈地址比堆地址低?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,堆栈地址高于进程地址空间中的堆地址.但是,当我编写一个程序在VS2010中进行验证时,遇到了一些麻烦.堆栈的地址低于堆的地址,甚至比数据段的地址还要低.该程序如下所示:

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));
}

根据VS2010,这是他们的地址:

Here are their address according to the VS2010:

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

&b      0x002af7a8
c       0x002af778

p1      0x00571500
p2      0x00571540

很显然,指向堆上数组的指针p1的地址比堆栈上的& b大.那是为什么?

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:很抱歉因我的声誉欠佳而没有图片,或者我可以更清楚地描述我的问题.

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

推荐答案

众所周知,堆栈地址高于进程地址空间中的堆地址."

"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.

这篇关于为什么在Visual C ++中堆栈地址比堆地址低?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆