博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据结构和算法学习四,之内存
阅读量:6436 次
发布时间:2019-06-23

本文共 1231 字,大约阅读时间需要 4 分钟。

void process(){    char* point = (char*)malloc(100);    free(point);}

 

引自:

内存是程序运行的基础。所有正在运行的代码都保存在内存里面。内存需要处理各种各样的数据,包括键盘的数据、鼠标的数据、usb的数据、串口的数据、摄像头的数据,那么这些数据经过程序的处理之后,就要进行输出到串口、屏幕、usb等。

    内存只有一个,但是程序里面的空间有很多种。但是内存中的数据类型只有几种,比如说全局中的数据、堆中的数据、临时堆栈中的数据。那么他们有什么区别呢?我们可以通过代码发现一些问题。

    (1)全局数据

 

static int value = 100;void process(){    static int number = 10;}

 

 大家可以在这里看到,value和number的数据其实都属于全局数据,这里的变量是不随着函数的调用发生变化的。

(2)堆数据

 

void process(){    char* point = (char*)malloc(100);    free(point);}

 

        这里的point分配的数据就是堆数据,如果没有free操作,那么它的存在也是全局的。只要内存不主动释放,那么这个内存就会以一直存在。

  (3)临时数据

 

void process(){    char name[100] = {
0}; return;}

 

  这里的数据都是堆栈内部的数据,一旦process调用结束返回之前,那么name地址指向的内存空间已经被其他函数使用。此时这段内存空间对我们来说已经没有什么意义了。所以,不管在函数里面用了多少空间,如果你想在函数返回之前继续使用里面的数据,务必在函数返回前拷贝完毕。

 

    这篇博客的内容比较简单,主要讲述了内存的一些内容。其实关于内存的东西还很多。这里说明一下只是让大家有一个了解:

    1) 全局数据是我们喜欢使用的类型,用起来比较方便

    2)堆数据是系统给我们安排的空间

    3)堆栈空间只能存在于当时的函数之中,函数返回即失去意义

    

    虽然我们上面这么说,但是这三个概念有的时候也是可以相互迁移的,比如说:

    1) 有的时候,我们为了测试的需要,首先构建一个全局内存池,以后测试的内存都是通过自定义的malloc在内存池中分配的,所以这个时候,堆分配和全局联系在了一起。

                全局内存空间          < =========>  内存池     < =========> 本地空间分配

    2) 如果我们使用的函数空间比较小,那么所有的操作就可以在一个函数内部完成了,那么这时候全局空间和临时堆栈是不是一致的呢

               全局空间   < =============>  本地堆栈

 

    上面的说法有些绕,但是我们的目的只是想让大家时刻明白:

    a)必须时刻明白我们的数据在哪块空间里面

    b)内存会不会越界

    c)内存会不会泄露

    d)内存访问的数据是否依然有效

    

【预告: 下面的博客开始介绍线性空间的内容】

   

    

 

 

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

 

转载地址:http://gaqga.baihongyu.com/

你可能感兴趣的文章
自然语言处理hanlp的入门基础
查看>>
JavaScript字符串操作方法大全,包含ES6方法
查看>>
UCloud想吃科创板的“第一个螃蟹”
查看>>
docker 入门应用
查看>>
一次java导出pdf的经历
查看>>
光控华登王毅喆:如何投资科技领域?坚守技术驱动,敬畏并影响周期
查看>>
PostgreSQL 与 MySQL 常用命令对照
查看>>
IIS崩溃时自动抓取Dump
查看>>
C# “贝格尔”编排法
查看>>
Android打包(一)
查看>>
15、【 商品管理模块开发】——后台获取商品详情功能开发及PropertiesUtil配置工具,DateTimeUtil时间处理工具开发...
查看>>
Linux服务器---安装bind
查看>>
Python 爬取斗图啦图片
查看>>
KDE Applications 19.04 发布,包含大量更新
查看>>
PHP mail()可能导致的问题
查看>>
[爬虫+数据分析] 分析北京Python开发的现状
查看>>
机器学习算法①
查看>>
CSS中的选择器之类选择器和id选择器
查看>>
【转载】MyBatis报错Invalid bound statement (not found)
查看>>
C#使用post提交http请求
查看>>