简单的内存管理类(关于malloc与free)

大家在写代码的时候是否有忘记free指针的时候呢?

如果是大型项目肯定会用一些比较稳定的开源库一类的来解决。而如果是私人的小项目,或者是测试一类的代码遇到这类问题还需要各种检查,是否很浪费时间呢?

给大家分享一个我个人使用的内存管理类哈~可以使用Create一块空间,之后可以不需要去管这块空间最后是否free掉,因为类析构时候会自动帮助free掉,而如果想要手动free也支持,直接传入申请空间时候取得的ID就可以指定析构了。这个还是比较适合私人用用哈~

首先是头文件~

 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
 * Author : sohunjug
 * Email : sohunjug@hotmail.com
 * Last modified : 2013-04-07 18:34
 * Filename : MemCtrl.h
 * Description : 内存管理类,类析构时自动释放内存。
 *               用于给指针申请内存空间。
 * *****************************************************************************/
#ifndef __MEMCTRL_H__
#define __MEMCTRL_H__
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>

class CMemCtrl
{
    private:
        typedef struct _DATA_SPACE_
        {
            void* point;
            _DATA_SPACE_* Next;
            _DATA_SPACE_* Pre;
            int count;

            _DATA_SPACE_()
            {
                point = NULL;
                Next = NULL;
                Pre = NULL;
                count = 0;
            }
            ~_DATA_SPACE_()
            {
                if (point != NULL)
                    free(point);
                if (Pre != NULL && Next == NULL)
                    Pre->Next = NULL;
                else if (Pre == NULL && Next != NULL)
                    Next->Pre = NULL;
                else if (Pre != NULL && Next != NULL)
                {
                    Pre->Next = Next;
                    Next->Pre = Pre;
                }
            }
            void SetPre(_DATA_SPACE_* Pre)
            {
                this->Pre = Pre;
            }
        }KDataSpace;

        typedef KDataSpace* PDataSpace;
    public:
        CMemCtrl();
        ~CMemCtrl();

        //申请空间
        void* Create(int x, int size);
        //指定ID释放空间
        void Clear(int count);
        //释放所有已申请空间
        bool Reset();
        //取得最近一次所申请的空间ID
        int GetCount();
    private:
        void Clear();
        void* point_one;
        std::vector<int> vCount;
        PDataSpace header;
        PDataSpace end;
        PDataSpace point;
        int count_one;
};

#endif

下面就是具体的实现了~

  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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
/*******************************************************************************
 * Author : sohunjug
 * Email : sohunjug@hotmail.com
 * Last modified : 2013-04-07 18:34
 * Filename : MemCtrl.h
 * Description : 内存管理类,类析构时自动释放内存。
 *               用于给指针申请内存空间。
 * *****************************************************************************/
#include "MemCtrl.h"

CMemCtrl::CMemCtrl()
{
    header = NULL;
    end = NULL;
    count_one = 0;
}

CMemCtrl::~CMemCtrl()
{
    Clear();
}

void CMemCtrl::Clear()
{
    while(header != NULL)
    {
        point = header->Next;
        delete header;
        header = point;
    }
    header = NULL;
    end = NULL;
    count_one = 0;
}

void CMemCtrl::Clear(int count)
{
    PDataSpace temp;
    temp = end;
    point = temp;
    while(point != NULL && point->count != count)
    {
        point = temp->Pre;
        temp = point;
    }
    vCount.push_back(point->count);
    if (point == header)
        header = header->Next;
    if (point == end)
        end = end->Pre;
    if (point != NULL)
    {
        delete point;
    }

    point = NULL;
}

int CMemCtrl::GetCount()
{
    return end->count;
}

void* CMemCtrl::Create(int x, int size)
{
    std::vector<int>::iterator vCountIte;
    point_one = NULL;
    point_one = (void*)calloc(x, size);
    if (point_one == NULL)
        return NULL;
    point = new KDataSpace();
    if (point == NULL)
        return NULL;
    point->point = point_one;
    while ((vCountIte = find(vCount.begin(), vCount.end(), count_one-1)) != vCount.end()) 
    {
        count_one--;
        vCount.erase(vCountIte);
    }
    if (vCount.size() > 0)
    {
        point->count = vCount[0];
        vCount.erase(vCount.begin());
    }
    else
    {
        point->count = count_one;
        count_one ++;
    }
    
    if (header == NULL)
    {
        header = point;
        end = point;
    }
    else
    {
        end->Next = point;
        point->SetPre(end);
        end = point;
    }
    
    return point_one;
}

bool CMemCtrl::Reset()
{
    Clear();
    return true;
}

comments powered by Disqus