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. Kubernetes 1.5集成heapster

    Heapster是kubernetes集群监控工具.在1.2的时候,kubernetes的监控需要在node节点上运行cAdvisor作为agent收集本机和容器的资源数据,包括cpu.内存.网络.文 ...

  2. 序列内第k小查询(线段树)

    最近请教了一下大佬怎么求序列内第k大查询,自己又捣鼓了一下,虽然还没有懂得区间第k大查询,不过姑且做一个记录先吧 因为每个元素大小可能很大而元素之间不连续,所以我们先离散化处理一下,程序中的ori[ ...

  3. saltsack自动化配置day03:服务部署mysql部署

    一.MySQL集群需求分享 1.抽象:功能模块 把基础的写成通用 服务部署也要抽象出来模块 redis内存有的多,有的少,可以config set在线更改 redis 安装.配置.启动 mysql 安 ...

  4. js原型解释图

  5. Sql Server 优化技巧

    1.查看执行时间和cpu占用时间 set statistics time on select * from dbo.Product set statistics time off 打开你查询之后的消息 ...

  6. mysql 主从配置(master/slave)

    1.  在每台服务器上创建复制账号(也可以只在master上创建用户,这里配置两个是为了方便以后切换) 备库运行的I/O县城需要建立一个到主库的TCP/IP连接,所以必须在主库创建一个用户,并赋予合适 ...

  7. HTTP请求方式及状态码

  8. Redis实战(四)CentOS 7上Redis哨兵

    什么是哨兵 顾名思义,哨兵的作用就是对Redis的系统的运行情况的监控,它是一个独立进程.它的功能有2个: 1. 监控主数据库和从数据库是否运行正常: 2. 主数据出现故障后自动将从数据库转化为主数据 ...

  9. asp启用父路径

    开启父路径后可以用../来表示上一层目录,如果网站程序中使用了../,不开启则网站程序里有../就会报错. IIS6启用父路径方法:打开IIS管理器——网站——右键属性——主目录——配置——选项——选 ...

  10. [csp-201509-3]模板生成系统

    #include<bits/stdc++.h> using namespace std; ; string a[N],b[N],c[N]; int main() { //freopen(& ...