• 数据结构和算法学习四,之内存


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

    引自:http://blog.csdn.net/feixiaoxing/article/details/6839301

    内存是程序运行的基础。所有正在运行的代码都保存在内存里面。内存需要处理各种各样的数据,包括键盘的数据、鼠标的数据、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)内存访问的数据是否依然有效

        

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

       

        

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

  • 相关阅读:
    jqgrid 使用入门
    bootstrap build
    bootstrap ace
    前序中序后序
    蓝桥杯-【交换瓶子】
    蓝桥杯-【剪邮票】【2016年省赛B组题解】--bfs+全排列
    蓝桥杯-【方格填数】【2016年省赛B组题解】
    2018年第九届蓝桥杯【C++省赛B组】【第十题:乘积最大】—贪心算法
    2018年第九届蓝桥杯【C++省赛B组】 第九题——全球变暖(bfs+queue)
    2018年第九届蓝桥杯【C++省赛B组】【第八题:日志统计】尺取法
  • 原文地址:https://www.cnblogs.com/Anita9002/p/4560903.html
Copyright © 2020-2023  润新知