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. 最短路 dijkstra 优先队列

    1.裸题 hdu2544 http://acm.hdu.edu.cn/showproblem.php?pid=2544 Way1: 好像不对 #include <cstdio> #incl ...

  2. Qt error ------ incomplete type 'QApplication' used in nested name specifier

    没有包含 ‘QApplication’ 头文件

  3. nginx访问日志出现大量的500状态信息,用postman返回 Internal Server Error,Too Many Attempts.错误的解决办法

    用postman的post方法访问某个URL时,出现以下错误: { "status": "1", "message": " Int ...

  4. RCNN,fast R-CNN,faster R-CNN

    转自:https://www.cnblogs.com/skyfsm/p/6806246.html object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别. ...

  5. vue学习记录

    vue中常用的指令 v-model 双向数据绑定,一般用于表单元素 v-for 对数组或对象进行循环操作,使用的是v-for <!-- 普通循环 --><li v-for=" ...

  6. Vuex深入理解

    store下的index.js: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) let store = new Vuex.St ...

  7. bzoj千题计划156:bzoj1571: [Usaco2009 Open]滑雪课Ski

    http://www.lydsy.com/JudgeOnline/problem.php?id=1571 DP不一定全部全状态转移 贪心的舍去一些不合法的反而更容易转移 在一定能力范围内,肯定滑雪所需 ...

  8. DFS——>记忆化搜索——>动态规划

    以洛谷P1802  5倍经验日 为例 https://www.luogu.org/problem/show?pid=1802 题目背景 现在乐斗有活动了!每打一个人可以获得5倍经验!absi2011却 ...

  9. Writing your first academic paper

    Writing your first academic paper If you are working in academics (and you are if you are working wi ...

  10. 来自一个Backbone的Hello,World!

    MVC写这种程序真是够大材小用的了,可没想到居然这么抽象! // 这是一个管理者视图/控制/模型 的全局类 var App = { Models: {}, Views: {}, Controllers ...