写模板的时候,模板的限制让YourClass [BUF]mem_pool
这样简单的办法失效了,我就问老师该如何捕捉内存然后“自动”管理。
老师说:C++的缺陷就在这里,你不能自己让内存释放。
于是神说:要让内存管理出现。
本文只是简单的内存管理(按照map的能力,同时存在十万级别的指针没有太大问题),如果你想要项目级别的内存管理,还是算了吧。
只需要让所有需要内存管理的类继承MemoryManagable
这个属性即可。
#define DEBUG
#define MEMMANAGER
#include <iostream>
#include <map>
# ifdef DEBUG
int globtest = 0;
# endif
namespace MatrixSpace
{
...
// 定义一个基类,利用基类指针可以指向子类指针的特性
class MemoryManagable
{
public:
MemoryManagable() {}
virtual ~MemoryManagable() {}
};
//让 Matrix继承MemoryManagable的特性
template <typename MatrixType>
class Matrix: public MemoryManagable
{
...
};
}
// 内存管理器
class MemoryManager
{
private:
std::map<MatrixSpace::MemoryManagable*, bool> mem_maps;
public:
void addToMap(MatrixSpace::MemoryManagable* mat_p)
{
if (mem_maps.count(mat_p)) {
throw std::domain_error("This pointer already registered in memory manager!!");
}
mem_maps[mat_p] = true;
return ;
}
void deleteFromMap(MatrixSpace::MemoryManagable* mat_p)
{
if (!mem_maps.count(mat_p)) {
throw std::out_of_range("This pointer is not in memory manager now..");
}
try {
delete mat_p;
mem_maps.erase(mat_p);
} catch (...) {
throw ;
}
return ;
}
void clearMap()
{
for (auto node : mem_maps) {
try {
delete node.first;
} catch (...) {
throw ;
}
}
mem_maps.clear();
return ;
}
} mem_manager;
namespace MatrixSpace
{
...
Matrix <MT> *res_mat = new Matrix <MT>(row, col);
//在你需要的地方加上对指针的管理
# ifdef MEMMANAGER
mem_manager.addToMap(res_mat);
# endif
...
}
using namespace MatrixSpace;
int main()
{
std::ios::sync_with_stdio(false);
using std::cout;
using std::endl;
Matrix <int> *A = new Matrix <int>(4, 5);
mem_manager.addToMap(A);
Matrix <int> *B = new Matrix <int>(4, 5);
mem_manager.addToMap(B);
cout << "Please input a Matrix <int> (4 * 5)" << endl;
A->initfs();
cout << "Please input a Matrix <int> (4 * 5)" << endl;
B->initfs();
(*A)[0][0] = 123;
Matrix <int> *C = (*((*A) + (*B))) + (*A);
cout << "A+B:" << endl << *C << endl;
// delete C;
C = nullptr;
C = (*A) - (*B);
cout << "A-B:" << endl << *C << endl;
C = nullptr;
A = nullptr;
B = nullptr;
# ifdef MEMMANAGER
//在不需要所有结果的时候,直接清除
mem_manager.clearMap();
# endif
# ifdef DEBUG
cout << globtest << endl;
# endif
return 0;
}
mem_manager
可以不止有一个,所以继续写的话,还是可以扩展一二的。