windows NT系统是以页尾基础的虚拟内存系统,使用32位线性地址。
在内部,系统管理被称为页的4096字节段中的所有内存。每页的物理内存都被备份。
临时的内存页使用页文件(pagefile),只读的内存页使用磁盘文件。同一时刻,最多可有16个不同的页文件。
代码,资源和其他只读数据通过他们创建的文件直接备份。
// Memory.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int g1=1,g2=2,g3=3; //define global variables
int _tmain(int argc, _TCHAR* argv[])
{
static int s1=1,s2=2,s3=3; //define the ststical variables
int l1=1,l2=2,l3=3;
//print the addresses of global variables
printf("0x%08x/n",&g1);
printf("0x%08x/n",&g2);
printf("0x%08x/n/n",&g3);
//print the addresses of statical variables
printf("0x%08x/n",&s1);
printf("0x%08x/n",&s2);
printf("0x%08x/n/n",&s3);
//print the addresses of local variables
printf("0x%08x/n",&l1);
printf("0x%08x/n",&l2);
printf("0x%08x/n",&l3);
scanf_s("%d",&l1);
return 0;
}
全局变量和静态变量分配的内存地址是连续的,但是局部变量却相差很远,局部变量分配在不同类型的内存区域中。
对于一个进程的内存空间而言,逻辑上分3个区域:代码区域,静态数据区域,动态数据区域。
动态数据是“堆栈”,堆是链式结构,栈是线性结构。
进程的每个线程都有私有的栈,所以每个线程即使代码一样,本地变量的数据互不干扰。
一个堆栈可由基地址和栈顶地址描述。
程序通过堆栈的基地址和偏移量访问局部变量。