C++——内存池(链式队列)

本文详细探讨了如何使用C++实现内存池,重点在于链式队列的数据结构。通过分析Mqueue.h头文件和main.cpp源代码,展示了内存池在动态内存分配和管理上的高效策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mqueue.h

#ifndef MQUEUE_H
#define MQUEUE_H
#include <vector>
#include<mutex> //加锁的头文件
#define LEN 10

//mutex m;
//一级 >128字节 系统申请
//二级 <128字节 内存管理
//内存池
template<typename T>
class Mqueue
{
public:
	class Node;
	Mqueue()
	{
		_head = new Node();
		_head->_next = NULL;
		_tail = _head;
	}
	~Mqueue()
	{
		while(!isEmpty())
		{
			pop();
		}
		delete _head;
		_head = _tail = NULL;
	}
	void push(T val)
	{
		_tail->_next = new Node(val); //申请内存
		_tail = _tail->_next;
		_tail->_next = NULL;
	}
	void pop()
	{
		if(isEmpty())
		{
			throw "queue is empty"; //抛出异常
		}
		Node *tmp = _head->_next;
		_head->_next = _head->_next->_next;
		if(_head->_next == NULL)
		{
			_tail = _head;
		}
		delete tmp;
	}
	T top()
	{
		return _head->_next->_val;
	}
	bool isEmpty()
	{
		return _head == _tail;
	}

private:
	class space
	{
	public:
		static space* getSpace()
		{
			if(_space == NULL)
			{
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值