zoukankan      html  css  js  c++  java
  • 内存管理链式

    mem.h

    #include <stdio.h>
    #include <stdlib.h>
    
    void *mymalloc(size_t size);
    void myfree(void *p);
    void *myrealloc(void*p, size_t size);
    
    //记录分配的内存地址大小,地址
    struct MEM
    {
        void *p;
        int size;
    
    };
    
    typedef struct LinkNode
    {
        struct MEM *pmem;//保存指针
        struct LinkNode *pNext;//指向下一个节点
    }node ,*PNODE;
    
    struct LinkNode *phead;
    
    
    PNODE addback(PNODE  phead, struct MEM *pmem);//插入一个数据
    PNODE change(PNODE phead, void *pfind, struct MEM *pnewmem);//修改
    PNODE findit(PNODE phead, void *pfind);
    PNODE deletenode(PNODE phead, void *paddr);//删除
    void showall(PNODE phead);
    
    void getinfo(PNODE phead);
    
    PNODE deleteall(PNODE phead);

    mem.c

    #include "mem.h"
    struct LinkNode *phead = NULL;
    
    
    void *mymalloc(size_t size)
    {
        void *p = malloc(size);
        //printf("
    分配内存地址%p,大小%d", p, size);
        struct MEM *pmem = malloc(sizeof(struct MEM));
        pmem->p = p;
        pmem->size = size;//开辟一个节点
        phead = addback(phead, pmem);
        return p;
    }
    
    void myfree(void *p)
    {
        //printf("
    内存地址%p释放", p);
    
        PNODE *px = findit(phead, p);
        if (px==NULL)
        {
            return;
        } 
        else
        {
            deletenode(phead, p);//删除
            free(p);
        }
    
    
    
        
    }
    void *myrealloc(void*p, size_t size)
    {
    
        void *pt = realloc(p, size);
        struct MEM mymem;
        mymem.p = pt;
        mymem.size = size;//新的内存地址大小
        phead = change(phead, p, &mymem);
    
    
        //printf("
    内存地址%p重新分配到%p,大小%d", p, pt, size);
        return pt;
    }
    PNODE addback(PNODE  phead, struct MEM *pmem)//插入一个数据
    {
        //struct MEM * 
        PNODE pnew = malloc(sizeof(node));
        pnew->pmem = pmem;
        pnew->pNext = NULL;//开辟一个节点
    
        if (phead==NULL)
        {
            phead = pnew;
        } 
        else
        {
            PNODE ptemp = phead;
            while (ptemp->pNext!=NULL)
            {
                ptemp = ptemp->pNext;
            }
            ptemp->pNext = pnew;
    
    
        }
    
    
        return phead;
    
    
    
    
    }
    PNODE change(PNODE phead, void *pfind, struct MEM *pnewmem)//修改
    {
        for (PNODE p = phead; p != NULL;p=p->pNext)
        {
            if (p->pmem->p==pfind)
            {
                p->pmem->p = pnewmem->p;
                p->pmem->size = pnewmem->size;
                return phead;
    
            }
    
        }
        return phead;
    }
    PNODE findit(PNODE phead, void *pfind)
    {
    
    
        for (PNODE p = phead; p != NULL; p = p->pNext)
        {
            if (p->pmem->p == pfind)
            {
            
                return p;
    
            }
    
        }
        return NULL;
    
    
    }
    PNODE deletenode(PNODE phead, void *paddr)//删除
    {
    
        PNODE p1, p2;
        p1 = p2 = NULL;
        p1 = phead;
        while (p1!=NULL)
        {
            if (p1->pmem->p ==paddr)
            {
                break;
            } 
            else
            {
                p2 = p1;
                p1 = p1->pNext;//备份上一个地址
            }
    
        }
        if (p1!=phead)
        {
            p2->pNext = p1->pNext;
            free(p1);
    
        } 
        else
        {
            phead = phead->pNext;
            free(p1);
        }
    
    
        return phead;
    
    
    }
    void showall(PNODE phead)
    {
    
        if (phead ==NULL)
        {
            return;
        } 
        else
        {
            printf("
    memaddr=%p,size=%d", phead->pmem->p, phead->pmem->size);
            showall(phead->pNext);//遍历
        }
    }
    
    void getinfo(PNODE phead)
    {
        printf("
    
    
    ");
        int i = 0;//多少个地址
        int j = 0;//代表多少个字节内存
        for (PNODE p = phead; p != NULL; p = p->pNext)
        {
            i++;
            j += p->pmem->size;
            printf("
    memaddr=%p,size=%d", p->pmem->p, p->pmem->size);
    
        }
        printf("
    目前一共%d个地址在使用,一共消耗%d内存", i, j);
    
    
    
    }
    
    PNODE deleteall(PNODE phead)
    {
        if (phead==NULL)
        {
            return NULL;
        } 
        else
        {
            PNODE p1 = NULL;
            PNODE p2 = NULL;
            p1 = phead;
            while (p1->pNext!=NULL)
            {
                p2 = p1->pNext;
                p1->pNext = p2->pNext;
                free(p2->pmem->p);//释放内存
                free(p2);
    
                //p1 = p1->pNext;
            }
            free(phead->pmem->p);//释放内存
            free(phead);//最后释放
    
            return NULL;
        }
    }

    main.c

    #include "mem.h"
    
    #define malloc mymalloc
    #define free myfree
    #define realloc myrealloc
    
    
    void main()
    {
        int a;
        int *p;
    
        void *p1 = malloc(1024 * 1024 *100);
        void *p2 = malloc(1024 * 1024 * 100);
        void *p3 = malloc(1024 * 1024 * 100);
        void *p4 = malloc(1024 * 1024 * 100);
        
        getinfo(phead);
    
    
        realloc(p1, 1024 * 1024 * 200);
        
    
        getinfo(phead);
    
    
        free(p2);
    
        free(p2);
        free(10003);
    
        getinfo(phead);
    
        deleteall(phead);
    
        system("pause");
    }
  • 相关阅读:
    AJPFX解析成员变量和局部变量
    AJPFX关于Java Object类常用方法小总结
    AJPFX关于面向对象中的对象初始化整理,综合子父类、代码块等等
    AJPFX总结FileWriter类的write方法
    AJPFX总结java创建线程的三种方式及其对比
    java android中日期时间 问题总结
    安卓开发——ListView控件(初始化ListView、列表刷新、长按添加menu)
    android开发分辨率适配总结
    activity生命周期实例(不同启动模式)
    ViewPage最全解析
  • 原文地址:https://www.cnblogs.com/sjxbg/p/5756546.html
Copyright © 2011-2022 走看看