(转)程序是怎么在内存中存储的
2015-04-08 15:42
218 查看
一个程序占有的内存分为5类:
1. 全局/静态数据区---->对应 .data数据段
2.常量数据区--> .rdata只读数据段
3.代码区---> .text代码段 (存储代码)
4.栈
5.堆
内存存储情况:
1. 全局/静态数据区---> 全局/静态数据
2.常量数据区(.rdata)-->常量字符串
3.栈---->自动变量或者局部变量,以及传递的函数参数
4.堆--->用户控制 new/malloc出来的内存,注意内存泄露问题
下面通过具体代码查看数据的存储问题:
[b][cpp] view
plaincopy[/b]
#include <stdio.h>
#include <stdlib.h>
int nGlobal = 200;
int main()
{
char *pLocalString1 = "LocalString1";
const char *pLocalString2 = "LocalString2";
static int nLocalStatic = 100;
int nLocal = 1;
const int nLocalConst = 20;
int * pNew = (int *)malloc(sizeof(int)*5);;
char *pMalloc = (char *)malloc(1);
//全局/静态数据区
printf("全局/静态数据区:/n");
printf("global variable: 0x%x/n", &nGlobal);
printf("static variable: 0x%x/n/n", &nLocalStatic);
//常量区
printf("常量区:/n");
printf("pLocalString1 variable: 0x%x/n", pLocalString1);
printf("pLocalString2 variable: 0x%x/n", pLocalString2);
printf("nLocalConst variable: 0x%x/n/n", nLocalConst);
//堆
printf("堆:/n");
printf("new variable: 0x%x/n", pNew);
printf("malloc variable: 0x%x/n/n", pMalloc);
//栈
printf("栈:/n");
printf("pointer pnew variable: 0x%x/n", &pNew);
printf("pointer malloc variable: 0x%x/n", &pMalloc);
printf("nLocal variable: 0x%x/n", &nLocal);
printf("pointer pLocalString1 variable: 0x%x/n", &pLocalString1);
printf("pointer pLocalString2 variable: 0x%x/n", &pLocalString2);
printf("nLocalConst variable: 0x%x/n/n", &nLocalConst);
return 0;
}
下面是运行结果:
我们可以看到栈和堆的内存都存在内存对齐的。 堆的内存是按照34个字节对齐(我的机器是这样的)的。
下面是各个数据所在区间的对应关系:
转自:http://blog.csdn.net/icekingson/article/details/6461629
1. 全局/静态数据区---->对应 .data数据段
2.常量数据区--> .rdata只读数据段
3.代码区---> .text代码段 (存储代码)
4.栈
5.堆
内存存储情况:
1. 全局/静态数据区---> 全局/静态数据
2.常量数据区(.rdata)-->常量字符串
3.栈---->自动变量或者局部变量,以及传递的函数参数
4.堆--->用户控制 new/malloc出来的内存,注意内存泄露问题
下面通过具体代码查看数据的存储问题:
[b][cpp] view
plaincopy[/b]
#include <stdio.h>
#include <stdlib.h>
int nGlobal = 200;
int main()
{
char *pLocalString1 = "LocalString1";
const char *pLocalString2 = "LocalString2";
static int nLocalStatic = 100;
int nLocal = 1;
const int nLocalConst = 20;
int * pNew = (int *)malloc(sizeof(int)*5);;
char *pMalloc = (char *)malloc(1);
//全局/静态数据区
printf("全局/静态数据区:/n");
printf("global variable: 0x%x/n", &nGlobal);
printf("static variable: 0x%x/n/n", &nLocalStatic);
//常量区
printf("常量区:/n");
printf("pLocalString1 variable: 0x%x/n", pLocalString1);
printf("pLocalString2 variable: 0x%x/n", pLocalString2);
printf("nLocalConst variable: 0x%x/n/n", nLocalConst);
//堆
printf("堆:/n");
printf("new variable: 0x%x/n", pNew);
printf("malloc variable: 0x%x/n/n", pMalloc);
//栈
printf("栈:/n");
printf("pointer pnew variable: 0x%x/n", &pNew);
printf("pointer malloc variable: 0x%x/n", &pMalloc);
printf("nLocal variable: 0x%x/n", &nLocal);
printf("pointer pLocalString1 variable: 0x%x/n", &pLocalString1);
printf("pointer pLocalString2 variable: 0x%x/n", &pLocalString2);
printf("nLocalConst variable: 0x%x/n/n", &nLocalConst);
return 0;
}
下面是运行结果:
我们可以看到栈和堆的内存都存在内存对齐的。 堆的内存是按照34个字节对齐(我的机器是这样的)的。
下面是各个数据所在区间的对应关系:
转自:http://blog.csdn.net/icekingson/article/details/6461629
相关文章推荐
- float数据在内存中是怎么存储的 AND IEEE754测试程序
- [VB.NET]点net写client程序传递参数给mssql存储过程insert,为什么要反复执行客户端代码才能成功insert,怎么才能使客户端插入记录操作变得稳定呢?
- WinCE动态调整存储内存与程序内存的大小
- C语言 如何查看数据在内存中是怎么存储的
- 程序在内存中存储详情(Text、Data、BSS、Heap、Stack....)
- 设定存储内存和程序内存的大小
- 以十六进制存储的文本文档正确读入内存的转化程序
- 程序的内存及标识符的存储属性(二)
- [原创]怎么降低Winform程序占用的内存
- gridview图片过大,连续几次打开关闭程序就会溢出内存该怎么办
- 程序的变量怎么存储的?
- 修改wince存储内存和程序内存
- Linux下C/C++程序内存布局 各种类型数据存储区域及生长方向
- Wince程序内存和存储内存
- Wince程序内存和存储内存--转载
- 怎么处理ADD-ON在程序中占的内存大的问题
- wince 程序内存与存储内存
- WINCE--程序内存和存储内存
- 程序在内存中存储情况:栈,堆,代码区,全局(静态)区 ,常量区
- 程序的内存及标识符的存储属性(一)