main.cpp

来自「内存池」· C++ 代码 · 共 74 行

CPP
74 行
字号
#include <iostream>
#include <string>
#include "CMemPool.h"

using namespace std;

//////////////////////////////////////////////////////////////////////////
class Test1
{
public:
	Test1()
	{
		v[0] = 'A';
	};
	char v[200];
};
//------------------------
class Test2
{
public:
	Test2(const string &s)
	{
		v[0] = s;
	};
	string v[200];
};
//-----------------------
class Test3
{
public:
	Test3(int a, int b, int c)
	{
		k = (a + b) * c;
	};
	~Test3(){};

	inline int GetValue() { return k; }

private:
	int k;
};

//////////////////////////////////////////////////////////////////////////


int main(int argc, char *argv[])
{
	Test1 *v1 = NULL;
	Test2 *v2 = NULL;
	Test3 *v3 = NULL;

	//获得内存池实例
	CMemPool *MemPool = CMemPool::GetInstance();

	//给Test1类的对象分配内存
	v1 = MemPool->AllocMemory<Test1>();
	//检测是否分配成功、构造函数是否正确执行
	cout << v1->v[0] << endl;
	//释放内存
	MemPool->FreeMemory(v1);

	v2 = MemPool->AllocMemory<Test2>("Hello Memory Pool!");
	cout << v2->v[0] << endl;
	MemPool->FreeMemory(v2);

	v3 = MemPool->AllocMemory<Test3>(1,2,3);
	cout << v3->GetValue() << endl;
	MemPool->FreeMemory(v3);

	//释放内存池
	MemPool->Release();

	system("pause");
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?