自己写的一个c++管理buffer

//创建一个循环链表来作为接受对方数据的缓存
#include<iostream>
#include<string.h>
#include<assert.h>
#include<stdio.h>
typedef struct node
{
	char *data;
	char *id;
	bool is_data;
	struct node *next;
}buffer_node;

class buffer
{
private:
	buffer_node *bufferlist;
	bool is_full;
public:
	buffer();
	~buffer();
	char *getData();
	bool addData(const char *id,const char *message);
};

buffer::buffer()
{
	buffer_node **entry = &bufferlist;
//	buffer_node *head = bufferlist;
	for(int a = 0; a < 20; a++)
	{
		*entry = new buffer_node;                     //第一次:给bufferlist充值,后面次数为给节点next充值
		
		(*entry)->data = new char[300];
		(*entry)->id = new char[20];
		(*entry)->is_data = false;
		memset((*entry)->data, 0, 300);
		memset((*entry)->id, 0, 20);
		
		entry = &(*entry)->next;					//entry保存指向next指针的指针
	}
	*entry = bufferlist;							//next再次指向头指针
//	std::cout<<"constructor"<<std::endl;
}

buffer::~buffer()
{
	buffer_node *tmp = bufferlist;
	buffer_node *entry;
	for(int a = 0; a < 20; a++)
	{
		entry = tmp;		//第一次为获取bufferlist,后面则为使用entry获取
		delete[] entry->data;
		delete[] entry->id;
		tmp = entry->next;
		delete entry;		
	}
	entry = NULL;
	tmp = NULL;
	bufferlist = NULL;
}

char *buffer::getData()                 //申请了内存需要自己释放
{
	for(int a = 0; a < 20; a++)
	{
		if(bufferlist->is_data == true)
		{
			char *data = new char[strlen(bufferlist->data)+1];
			std::cout<<strlen(bufferlist->data)<<std::endl;
			strcpy(data, bufferlist->data);                      //id使用暂时未定
			memset(bufferlist->data, 0, 300);                      
			memset(bufferlist->id, 0, 20);              //使用后清零
			bufferlist->is_data = false;
			return data;
		}
		bufferlist = bufferlist->next;
	}
}

bool buffer::addData(const char *id,const char *message)    //为了使add后立刻能get使用数据,所以add不移动bufferlist指针
{
	assert(id != NULL && message != NULL);
	assert(strlen(id) < 19);
	buffer_node *tmp = bufferlist;
	for(int a = 0; a < 20; a++)
	{
		if(tmp->is_data == false)
		{
			strcpy(tmp->id, id);
			strcpy(tmp->data, message);
			tmp->is_data = true;			//原来的代码tmp->is_data == true; 手贱啊啊啊!!多打了一个=,崩溃!!
			return true;
		}
		tmp = tmp->next;
	}
	return false;
}

//测试
int main()
{
	buffer *read = new buffer;
	if(read->addData("55", "fuck") == false) std::cout<<"add erro"<<std::endl;
	//delete read;
	std::cout<<"1"<<std::endl;
	if(read->addData("55", "55safsdafsad") == false) std::cout<<"add erro"<<std::endl;
	std::cout<<"fdsff"<<std::endl;
	char *data1 = read->getData();
	char *data2 = read->getData();
	char *data3 = read->getData();
	std::cout<<data1<<data2<<std::endl<<data3<<"fd"<<std::endl;
//	printf("%p",data);
	delete data1,data2;
	delete read;
}


这个类创建时构造一个20个节点的链表,然后通过addData方法可以插入数据并把标记写为true表示有数据,通过getData方法获取数据并把标记置为false。get获得的内存指针需要自己释放内存,main为测试

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值