您的位置:首页 > 编程语言 > C语言/C++

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

2013-12-20 10:03 232 查看
//创建一个循环链表来作为接受对方数据的缓存
#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为测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐