资源描述:
《C语言内存管理及经典算法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C语言内存管理对于一个c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题,为了应对这个问题,有许多技术被研究出来来解决这个问题,例如SmartPointer,GarbageCollection等。一般我们常说的内存泄漏是指堆内存的泄漏。那么为什么会导致内存泄漏呢?通过学习内存管理,相信你一定能解决好这个问题。1-1C语言内存管理方式在进入本专题前,我们先看一下下面的程序,来简单分析以下C语言的内存管理:#include#include//全局变量定义intiGlobalInt1=0;intiGlobalInt
2、2=0;intiGlobalInt3=0;//全局常量定义constintiGlobalConstInt1=1;constintiGlobalConstInt2=5;constintiGlobalConstInt3=6;//全局静态变量定义staticintiGlobalStaticInt1=0;staticintiGlobalStaticInt2=0;staticintiGlobalStaticInt3=0;//函数定义voidfuncParamTest(intiFuncParam1,intiFuncParam2,intiFuncParam3){//函数
3、私有变量定义intiLocalInt1=iFuncParam1;intiLocalInt2=iFuncParam2;intiLocalInt3=iFuncParam3;printf("函数参数变量内存地址");printf("iFuncParam1=0x%08x",&iFuncParam1);printf("iFuncParam2=0x%08x",&iFuncParam2);printf("iFuncParam3=0x%08x",&iFuncParam3);printf("函数本地变量的内存地址");printf("iLocalIn
4、t1=0x%08x",&iLocalInt1);printf("iLocalInt2=0x%08x",&iLocalInt2);printf("iLocalInt3=0x%08x",&iLocalInt3);return;}//入口函数intmain(intargc,char*argv[]){//局部静态变量staticintiStaticInt1=0;staticintiStaticInt2=0;staticintiStaticInt3=0;//局部静态常量定义conststaticintiConstStaticInt1=0;consts
5、taticintiConstStaticInt2=0;conststaticintiConstStaticInt3=0;//局部常量constintiConstInt1=1;constintiConstInt2=5;constintiConstInt3=6;//局部变量intiLocalInt1=0;intiLocalInt2=0;intiLocalInt3=0;char*pMalloc1,*pMalloc2,*pMalloc3;char*pNew1,*pNew2,*pNew3;printf("全局常量的内存地址");printf("iGlobalCo
6、nstInt1=0x%08x",&iGlobalConstInt1);printf("iGlobalConstInt2=0x%08x",&iGlobalConstInt2);printf("iGlobalConstInt3=0x%08x",&iGlobalConstInt3);printf("iConstStaticInt1=0x%08x",&iConstStaticInt1);printf("iConstStaticInt2=0x%08x",&iConstStaticInt2);printf("iConstStaticInt3=0
7、x%08x",&iConstStaticInt3);printf("全局变量的内存地址");printf("iGlobalInt1=0x%08x",&iGlobalInt1);printf("iGlobalInt2=0x%08x",&iGlobalInt2);printf("iGlobalInt3=0x%08x",&iGlobalInt3);printf("静态变量的内存地址");printf("iGlobalStaticInt1=0x%08x",&iGlobalStaticInt1);printf("iGlobalSt
8、aticInt2=0x%08x",&iGlobalStatic