#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <list>#include <malloc.h>using namespace STD;classstring{ Public: String ():p TR (New Char[1]) {//cout<< "String ()" <<endl;ptr[0]=' + '; } ~string () {if(PTR) {Deleteptr ptr = NULL; }} String (Const Char*P):p TR (New Char[strlen(p) +1]) {strcpy(PTR,P); }voidPrintf () {if(PTR)cout<<ptr<<endl; } String (Conststring& s):p tr (New Char[strlen(S.PTR) +1]) {strcpy(PTR,S.PTR); } string&operator= (ConstString &s) {if( This!=&s) {if(PTR)Delete[]ptr; PTR = (Char*)malloc(strlen(S.PTR) +1);strcpy(PTR,S.PTR); } }Char&operator[](intSize) {returnPtr[size]; }Private:Char*ptr;};/// test classstructnode{void*_p; size_t _line;Char*_filename; size_t _size; Node (void*q = null,size_t len = size_t (),Const Char*name="",intD=int()): _p (Q), _line (len), _filename (New Char[strlen(name) +1]), _size (d) {strcpy(_filename,name); }};#define ALLOC (Type) (_alloc (sizeof (type), __file__,__line__,type ()))#define Allocarray (type,size) (__alloc (sizeof (type), Size,__file__,__line__,type ()))//Define macros cannot be overloaded, just encountered this problem, record, type is a type. list<Node>Mlist;//Call the STL to save a linked list.ostream&operator<< (ostream& OS,ConstNode &node)//Overload memory leak output information. {cout<<node._P<<"\ T";cout<<node._Line<<"\ T";cout<<node._Filename<<"\ T";cout<<node._Size<<"\ T";cout<<endl;}///////////////////////////////////////////////struct_false{BOOLIspod () {return false;//Type selection in another way.}};struct_true{BOOLIspod () {return true; }};//extraction.Template<TypeNameType>//Standardizingclasstriast{ Public:typedef_false _ispod_; };Template<>classtriast<int>//Special{ Public:typedef_true _ispod_;};Template<>//Special. classtriast<Char>{ Public:typedef_true _ispod_;};Template<>classtriast<Double>{ Public:typedef_true _ispod_;};Template<>classtriast<Long>{ Public:typedef_true _ispod_;};Template<>classtriast< Short>{ Public:typedef_true _ispod_;};/////////////////////////////////////////////////Static voidDeletearray (void*p,intSize) { list<Node>:: Iterator it; list<Node>:: Iterator Itsave; It=mlist.begin (); while(it->_p!=p) {it++; } it--; for(intI=0; i<size+1; i++) {itsave = it++;//Must save Itsave, let it++, then delete itsave, otherwise when we delete it, then it++,Mlist.erase (Itsave);//Will error, because it no longer exists.}};Template<TypeNameType>Static void* __ALLOC (intSizeintLenConst Char*_file_,int_line_,type S1) {//This is a non-STL type of pointers. typedef TypeNameTriast<type>:: _ispod_ ispod;void*p =malloc(size*len+4); (*((int*) (p)) =len; Node node (p,size,"0", _line_);The //cookie value is marked with "0" and is selected when printing.Mlist.push_back (node); p = (int*) p+1;if(!ispod (). Ispod ())//construction.{Type *Q = (type *) p; for(intI=0; i<len;i++) {node node (q,size,_file_,_line_); Mlist.push_back (node);New(q) type (); q+=1; }returnP }Else{Type *Q = (type *) p; for(intI=0; i<len;i++) {node node (p,size,_file_,_line_); Mlist.push_back (node); q+=1; }returnP }}///////////////////////////////////////////////////////////////////////////Template<TypeNameType>Static void* _ALLOC (intSizeConst Char*_file_,int_line_,_true,type S1) {//Do not need to call the constructor's cheat space in such a way as int *,char * void*p =malloc(size); Node node (p,size,_file_,_line_); Mlist.push_back (node);returnP }Template<TypeNameType>Static void* _ALLOC (intSizeConst Char*_file_,int_line_,_false,type S1) {//The way in which the skin space of the constructor needs to be called, such as: String S (); void*p =malloc(size); Node node (p,size,_file_,_line_); Mlist.push_back (node);New(p) type ();returnP;}Template<TypeNameType>Static void* _ALLOC (intSizeConst Char*_file_,int_line_,type S1)//stl kind of pointers.{typedef TypeNameTriast<type>:: _ispod_ ispod;return_alloc (Size,_file_,_line_,ispod (), S1);}Static voidPrintf () { list<Node>:: Iterator it; it = Mlist.begin (); while(It!=mlist.end ()) {if(strcmp(It->_filename,"0"))cout<<*it<<"\ T"; ++it; }}Template<TypeNameType>Static void_delete (type *p,_true) { list<Node>:: Iterator it; it = Mlist.begin (); while(1) {if(it->_p==p) {mlist.erase (IT); Break; } it++; }}Template<TypeNameType>Static void_delete (type *p,_false) { list<Node>:: Iterator it; it = Mlist.begin (); P->~type (); while(1) {if(it->_p==p) {mlist.erase (IT); Break; } it++; }}Template<TypeNameType>Static voidDELETE (Type *p)//This requires the type extraction, automatically determine the type of p, see if you need to call the destructor. {typedef TypeNameTriast<type>:: _ispod_ ispod; _delete (P,ispod ());}intMain () {//string *s = (String *) ALLOC (string); //int *a = (int *) ALLOC (sizeof (int) int); //delete (s); //int *b = (int *) ALLOC (int); //int *p = (int *) Allocarray (int,2);String *s = (String *) Allocarray (String,3);//Deletearray (s,3);//s[0]. Printf ();//s[1]. Printf ();//s[2]. Printf ();//allocarry (int,2);Printf ();//traits (s);//extraction test. return 0;}
Let's implement a memory management tool. Chapter II (Type, cookie information, construct, array)