下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
//让我们开始一个完整的内存管理工具的实现吧。///准备做一个完整的内存管理工具//涉及线程,内存池,萃取,不仅仅是new跟delete的重载(或者说是函数重载),这是我的一个雏形,大家谁有什么好的指正谢谢提出,一起学习。#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <List>#include <malloc.h>using namespace std;class String{ public: String():ptr(new char[1]) { cout<<"String()"<<endl; ptr[0]=''; } ~String() { if(ptr){ delete ptr; ptr = NulL; } } String(const char *p):ptr(new char[strlen(p)+1]) { strcpy(ptr,p); } voID Printf() { cout<<"liuhuiyan"<<endl; cout<<ptr<<endl; } String(const String& s):ptr(new char[strlen(s.ptr)+1]) { strcpy(ptr,s.ptr); } String& operator = (const String &s) { if(this!=&s) { if(ptr)delete []ptr; ptr = (char *)malloc(strlen(s.ptr)+1); strcpy(ptr,s.ptr); } } char& operator [](int size) { return ptr[size]; } private:char *ptr;};///测试类struct Node{ voID *_P; size_t _line; char *_filename; size_t _Size; Node(voID *q = NulL,size_t len = size_t(),\ const char *name="",int d=int()):_P(q),\ _line(len),_filename(new char[strlen(name)+1]),_Size(d) { strcpy(_filename,name); }};#define ALLOC(size,type) (_ALLOC(size,__file__,__liNE__,type()))//type是传进来的一个类型,List<Node> mList;//调用stl的一个链表保存.ostream& operator <<(ostream& os,const Node &node)//重载内存泄漏输出信息。{ cout<<node._P<<endl; cout<<node._line<<endl; cout<<node._filename<<endl; cout<<node._Size<<endl; }///////////////////////////////////////////////struct _false{};struct _true{};//萃取.template<typename type>//范化class triast{ public: typedef _false _ISPOD_ ; };template<>class triast<int>//特化{ public: typedef _true _ISPOD_;};template<>//特化。class triast<char>{ public: typedef _true _ISPOD_;};////////////////////////////////////////////相当与STL里面的萃取,别急,这只是我的一个雏形。template<typename type>class Traist{ public: typedef type _type_;};template<typename type>static voID traits(type &val){ typedef typename Traist<type> :: _type_ _TYPE_; *val="123"; val->Printf();}//类型萃取,我的目的是当我传入参数的时候,不需要传递它的类型。。/////////////////////////////////////////////////template<typename type>static voID* _ALLOC(int size,const char *_file_,int _line_,_true,type s1){ //不需要调用构造函数的赖皮空间的方式如int *,char * voID *p = malloc(size); Node node(p,size,_file_,_line_); mList.push_back(node); return p; }template<typename type>static voID* _ALLOC(int size,_false,type s1){ //需要调用构造函数的靠皮空间的方式如:String s(); voID *p = malloc(size); Node node(p,_line_); mList.push_back(node); new(p)type(); return p;}template<typename type>static voID* _ALLOC(int size,type s1){ typedef typename triast<type> :: _ISPOD_ ispod; _ALLOC(size,_line_,ispod(),s1);}static voID Printf(){ List<Node> :: iterator it; it = mList.begin(); while(it!=mList.end()) { cout<<*it<<"\t"; ++it; }}static voID DELETE(voID *p)//这里就需要类型萃取,自动判别P的类型,看是否需要调用构造函数。{ List<Node>::iterator it; it = mList.begin(); while(1) { if(it->_P==p){ mList.erase(it); break; } it++; }}int main(){ String *s = (String *)ALLOC(sizeof(String),String);//int *a = (int *)ALLOC(sizeof(int),int);// DELETE(a);// Printf(); traits(s);//萃取测试. return 0;}
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的C++实现一个完整的内存管理工具(线程,内存池,萃取)全部内容,希望文章能够帮你解决C++实现一个完整的内存管理工具(线程,内存池,萃取)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)