C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 
void free(void *firstbyte)
{
    struct mem_control_block *mcb;
    /* Backup from the given pointer to find the
    * mem_control_block
    */
    mcb = firstbyte - sizeof(struct mem_control_block);
    /* Mark the block as being available */
    mcb->is_available = ;
    /* That's It! We're done. */
    return;
}

void *malloc(long numbytes)
{
    /* Holds where we are looking in memory */  
    void *current_location;
      /* This is the same as current_location, but cast to a* memory_control_block*/  
    struct mem_control_block *current_location_mcb;
      /* This is the memory location we will return. It will* be set to 0 until we find something suitable*/  
    void *memory_location;
      /* Initialize if we haven't already done so */  
    if(!has_initialized)
    {
        malloc_init();
    }
    /* The memory we search for has to include the memory* control block,
    but the users of malloc don't need* to know this,
    so we'll just add it in for them.*/  
    numbytes = numbytes + sizeof(struct mem_control_block);

/* Set memory_location to 0 until we find a suitable* location*/  
    memory_location = ;

/* Begin searching at the start of managed memory */  
    current_location = managed_memory_start;

/* Keep going until we have searched all allocated space */  
    while(current_location != last_valid_address)      
    {
        /* current_location and current_location_mcb point* to the same address.
        However, current_location_mcb* is of the correct type, so we can use it as a struct.
        * current_location is a void pointer so we can use it* to calculate addresses.*/      
        current_location_mcb = (struct mem_control_block *)current_location;

if(current_location_mcb->is_available)          
        {
            if(current_location_mcb->size >= numbytes)              
            {
                /* Woohoo! We've found an open,* appropriately-size location.*/
                /* It is no longer available */
                current_location_mcb->is_available = ;

/* We own it */
                memory_location = current_location;

/* Leave the loop */
                break;
                  
            }          
        }      
        /* If we made it here, it's because the Current memory* block not suitable; move to the next one*/      
        current_location = current_location + current_location_mcb->size;
    }  

/* If we still don't have a valid location, we'll* have to ask the operating system for more memory*/  
    if(! memory_location)      
    {
        /* Move the program break numbytes further */
        sbrk(numbytes);
        /* The new memory will be where the last valid* address left off*/      
        memory_location = last_valid_address;
        /* We'll move the last valid address forward* numbytes*/
        last_valid_address = last_valid_address + numbytes;
        /* We need to initialize the mem_control_block */      
        current_location_mcb = memory_location;
        current_location_mcb->is_available = ;
        current_location_mcb->size = numbytes;
    }  
    /* Now, no matter what (well, except for error conditions),* memory_location has the address of the memory, including* the mem_control_block*/  
    /* Move the pointer past the mem_control_block */  
    memory_location = memory_location + sizeof(struct mem_control_block);
    
    /* Return the pointer */
    return memory_location;
}

malloc和free的实现的更多相关文章

  1. malloc 与 free函数详解<转载>

    malloc和free函数详解   本文介绍malloc和free函数的内容. 在C中,对内存的管理是相当重要.下面开始介绍这两个函数: 一.malloc()和free()的基本概念以及基本用法: 1 ...

  2. C 语言中 malloc、calloc、realloc 和free 函数的使用方法

    C标准函数库中,常见的堆上内存管理函数有malloc(), calloc(), recalloc(), free(). 之所以使用堆,是因为栈只能用来保存临时变量.局部变量和函数参数.在函数返回时,自 ...

  3. 以冒泡排序为例--malloc/free 重定向stdin stdout

    esort.c 代码如下,可关注下mallloc/free,freopen重定向的用法,排序为每轮将最小的数放在最前面: #include<stdio.h> #include<mal ...

  4. 内存动态分配之realloc(),malloc(),calloc()与new运算符

    1,malloc与free是C/C++的标准库函数,new/delete是C++的运算符,是C++面向对象的特征,它们都可用于申请动态内存和释放内存.2,对于非内部数据类型的对象而言,光用maloc/ ...

  5. 在dll里malloc/new/cvCreate分配内存,在exe里free/Releases释放内存时会出错。

    写了个程序,在DLL中用malloc分配了一块内存,但是在exe程序中释放,结果程序crash,原因就是:其原因可能是堆被损坏,这也说明 TestMySticker.exe 中或它所加载的任何 DLL ...

  6. Linux C 堆内存管理函数malloc()、calloc()、realloc()、free()详解

    C 编程中,经常需要操作的内存可分为下面几个类别: 堆栈区(stack):由编译器自动分配与释放,存放函数的参数值,局部变量,临时变量等等,它们获取的方式都是由编译器自动执行的 堆区(heap):一般 ...

  7. malloc与new的区别

    1.new是运算符,而malloc是库函数 2.new可以重载,可以自定义内存分配策略,甚至不做内存分配,甚至分配到非内存设备上.而malloc不能. 3.new在用于定义一个新的非内部对象的时候,默 ...

  8. new 等于 malloc加构造函数

    1.new 是c++中的操作符,malloc是c 中的一个函数 2.new 不止是分配内存,而且会调用类的构造函数,同理delete会调用类的析构函数,而malloc则只分配内存,不会进行初始化类成员 ...

  9. 关于malloc函数的动态分配问题

    malloc函数动态分配了一个整型的内存空间,让abc都指向刚申请的空间,所以只有最后一个赋值语句的值保留在了空间里 #include<stdio.h> main() { int *a,* ...

  10. 转:如何实现一个malloc

    如何实现一个malloc 转载后排版效果很差,看原文!   任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉. ...

随机推荐

  1. 3532: [Sdoi2014]Lis 最小字典序最小割

    3532: [Sdoi2014]Lis Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 865  Solved: 311[Submit][Status] ...

  2. Linux日常维护命令

    对于程序员来说,掌握一些基本的Linux命令是必不可少的,即使现在用不到,在不久的将来也应该会用到.由于Linux有很多命令,每个命令基本可以用一篇文章介绍,所以本文仅总结一些常用命令的常用用法,如有 ...

  3. C++中#define用法

    http://blog.sina.com.cn/s/blog_686188ef0100klku.html #define是C语言中提供的宏定义命令,其主要目的是为程序员在编程时提供一定的方便,并能在一 ...

  4. LigerUI下拉选择列表LigerComboBox中tree的节点初始化默认选中的问题

    问题描述 用后台的Json传送tree的数据 前端用js方法来控制选中节点 此方法存在bug 实例: bug问题:无论设置的默认选中值是多少个,前台checkbox最多只显示选中一个,但是内容框中显示 ...

  5. Matlab修改背景颜色

    步骤: 1, 在matlab命令行中运行prefdir, 获取matlab.prf文件所在路径 2, 打开matlab.prf所在路径, 找到matlab.prf文件, 作备份 3, 将以下内容添加到 ...

  6. KVM管理概述

    一.使用QEMU管理虚拟机 1.KVM指南 https://activedoc.opensuse.org/book/opensuse-virtualization-with-kvm/part-iii- ...

  7. 科学计算三维可视化---Traits(Event和button属性)

    Event和button属性 是两个专门用于处理事件的change属性 Event属性和其他Trait属性不一样 Button属性是由Event属性继承而来的 Event监听 from traits. ...

  8. Vue.js学习笔记(二)

    Vue.js不支持IE8及以下的版本,因为vue使用了IE8无法模拟的ECMAScript5的特性,它支持所有兼容ECMAScript5的浏览器. <!DOCTYPE html> < ...

  9. 在centos6.5安装pg

    环境:centos 6.5系统,连外网. 1.参考pg官方网站进行安装.(按照上面的命令行依次执行就行) https://www.postgresql.org/download/linux/redha ...

  10. 2018ICPC赛后总结

    南京: 南京是我们队第一场区域赛,而且和一队二队一起,心里总有种踏实感.之前我们仨在一起讨论过我们打大型比赛出现的问题,晨哥说我们在封榜后总是出不了题,确实是这样,我觉得在之前的比赛中,我们队胜负欲都 ...