没有合适的资源?快使用搜索试试~ 我知道了~
首页C语言内存管理与内存泄露
C语言内存管理与内存泄露
5星 · 超过95%的资源 需积分: 10 30 下载量 107 浏览量
更新于2023-03-03
评论
收藏 272KB DOC 举报
1-1 C语言内存管理方式 1-2 C语言内存管理 1-3 C语言内存使用要点及常见错误 1-4 在Windows下如何跟踪内存泄露 1-5 Windows内存管理简述 1-6 总结和建议读者的练习
资源详情
资源评论
资源推荐
C 语言内存管理
对于一个 c/c++程序员来说,内存泄漏是一个常见的也是令人头疼的问题,为了应对这
个问题,有许多技术被研究出来来解决这个问题,例如 Smart Pointer,Garbage Collection
等。一般我们常说的内存泄漏是指堆内存的泄漏。那么为什么会导致内存泄漏呢?通过学
习内存管理,相信你一定能解决好这个问题。
-1C 语言内存管理方式
在进入本专题前,我们先看一下下面的程序,来简单分析以下 C 语言的内存管理:
#include <stdio.h>
#include <malloc.h>
//全局变量定义
int iGlobalInt1=0;
int iGlobalInt2=0;
int iGlobalInt3=0;
//全局常量定义
const int iGlobalConstInt1=1;
const int iGlobalConstInt2=5;
const int iGlobalConstInt3=6;
//全局静态变量定义
static int iGlobalStaticInt1=0;
static int iGlobalStaticInt2=0;
static int iGlobalStaticInt3=0;
//函数定义
void funcParamTest(int iFuncParam1,int iFuncParam2,int iFuncParam3)
{
//函数私有变量定义
int iLocalInt1=iFuncParam1;
int iLocalInt2=iFuncParam2;
int iLocalInt3=iFuncParam3;
1
printf("函数参数变量内存地址\n");
printf("iFuncParam1=0x%08x\n",&iFuncParam1);
printf("iFuncParam2=0x%08x\n",&iFuncParam2);
printf("iFuncParam3=0x%08x\n\n",&iFuncParam3);
printf("函数本地变量的内存地址\n");
printf("iLocalInt1=0x%08x\n",&iLocalInt1);
printf("iLocalInt2=0x%08x\n",&iLocalInt2);
printf("iLocalInt3=0x%08x\n\n",&iLocalInt3);
return;
}
//入口函数
int main(int argc, char* argv[])
{
//局部静态变量
static int iStaticInt1=0;
static int iStaticInt2=0;
static int iStaticInt3=0;
//局部静态常量定义
const static int iConstStaticInt1=0;
const static int iConstStaticInt2=0;
const static int iConstStaticInt3=0;
//局部常量
const int iConstInt1=1;
const int iConstInt2=5;
const int iConstInt3=6;
//局部变量
int iLocalInt1=0;
int iLocalInt2=0;
int iLocalInt3=0;
char * pMalloc1,*pMalloc2,*pMalloc3;
char * pNew1,*pNew2,*pNew3;
printf("全局常量的内存地址\n");
printf("iGlobalConstInt1=0x%08x\n",&iGlobalConstInt1);
printf("iGlobalConstInt2=0x%08x\n",&iGlobalConstInt2);
printf("iGlobalConstInt3=0x%08x\n\n",&iGlobalConstInt3);
2
printf("iConstStaticInt1=0x%08x\n",&iConstStaticInt1);
printf("iConstStaticInt2=0x%08x\n",&iConstStaticInt2);
printf("iConstStaticInt3=0x%08x\n\n",&iConstStaticInt3);
printf("全局变量的内存地址\n");
printf("iGlobalInt1=0x%08x\n",&iGlobalInt1);
printf("iGlobalInt2=0x%08x\n",&iGlobalInt2);
printf("iGlobalInt3=0x%08x\n\n",&iGlobalInt3);
printf("静态变量的内存地址\n");
printf("iGlobalStaticInt1=0x%08x\n",&iGlobalStaticInt1);
printf("iGlobalStaticInt2=0x%08x\n",&iGlobalStaticInt2);
printf("iGlobalStaticInt3=0x%08x\n\n",&iGlobalStaticInt3);
printf("iStaticInt1=0x%08x\n",&iStaticInt1);
printf("iStaticInt2=0x%08x\n",&iStaticInt2);
printf("iStaticInt3=0x%08x\n\n",&iStaticInt3);
printf("本地变量的内存地址\n");
printf("iConstInt1=0x%08x\n",&iConstInt1);
printf("iConstInt2=0x%08x\n",&iConstInt2);
printf("iConstInt3=0x%08x\n\n",&iConstInt3);
printf("iLocalInt1=0x%08x\n",&iLocalInt1);
printf("iLocalInt2=0x%08x\n",&iLocalInt2);
printf("iLocalInt3=0x%08x\n\n",&iLocalInt3);
funcParamTest(iLocalInt1,iLocalInt2,iLocalInt3);
//在堆上分配内存,使用 new
pNew1=new char[16];
pNew2=new char[16];
pNew3=new char[16];
//在堆上分配内存,使用 malloc
pMalloc1 = (char *)malloc( 16 );
pMalloc2 = (char *)malloc( 16 );
pMalloc3 = (char *)malloc( 16 );
printf("在堆上分配内存内存地址\n");
printf("pMalloc1=0x%08x\n",pMalloc1);
printf("pMalloc2=0x%08x\n",pMalloc2);
printf("pMalloc3=0x%08x\n\n",pMalloc3);
//释放 new 分配的内存空间
3
delete [] pNew1;
delete [] pNew2;
delete [] pNew3;
pNew1=NULL;
pNew2=NULL;
pNew3=NULL;
//释放 malloc 分配的内存空间
free(pMalloc1);
free(pMalloc2);
free(pMalloc3);
pMalloc1=NULL;
pMalloc2=NULL;
pMalloc3=NULL;
return 0;
}
本程序在 Windows XP 下,VC6 编译后的执行结果是:
4
注意,上面我们输出的完全是内存地址,也就是说,是程序在进程中内存地址(注意是虚
拟内存地址而不是物理内存地址)。我们认真观察程序输出,发现每种类型的内存地址都
是连续的,而不同类型之间内存地址有的是连续的,有的差别极大(注意:不同编译器可
能输出的结果不一样,但这并不影响我们分析问题)。基本上,我们可以把这些地址范围
分为如下几个部分:堆、栈、全局/静态存储区和常量存储区。
栈,就是那些由编译器在需要的时候分配,在不需要的时候自动释放的存储区。里面的变
量通常是局部变量、函数参数等。在栈上分配内存,通常是指在执行函数时,函数内局部
变量在栈上创建,函数执行结束时这被自动释放。栈内存分配运算内置于处理器的指令集
中,效率很高,但是分配的内存容量有限。
5
剩余25页未读,继续阅读
yuyuyu_713
- 粉丝: 7
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- ExcelVBA中的Range和Cells用法说明.pdf
- 基于单片机的电梯控制模型设计.doc
- 主成分分析和因子分析.pptx
- 共享笔记服务系统论文.doc
- 基于数据治理体系的数据中台实践分享.pptx
- 变压器的铭牌和额定值.pptx
- 计算机网络课程设计报告--用winsock设计Ping应用程序.doc
- 高电压技术课件:第03章 液体和固体介质的电气特性.pdf
- Oracle商务智能精华介绍.pptx
- 基于单片机的输液滴速控制系统设计文档.doc
- dw考试题 5套.pdf
- 学生档案管理系统详细设计说明书.doc
- 操作系统PPT课件.pptx
- 智慧路边停车管理系统方案.pptx
- 【企业内控系列】企业内部控制之人力资源管理控制(17页).doc
- 温度传感器分类与特点.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论5