#pragma once//#include <iostream>//#include <string>//#include <list>//#include < assert.h>//using namespace std;//struct blockinfo//{//void* _ptr;//string _file;// Int _line;//blockinfo (void *ptr, const char*file, int line)//:_ptr (PTR)//, _file (file)//, _line (line)//{}//};////Singleton mode//class singleton//{//public://static singleton* getinstance ()//{//if (sinstance == null)//{//sinstance = new singleton;//} Return sinstance;//}//void *alloc (Size_t size, const char* file, int line)//{//void* ptr = malloc (size);//if (PTR)//{//blockinfo b (ptr, File, line);//getinstance ()->blocklist.push_back (b);//}//return ptr;//}//void dalloc (void *ptr)//{//free (PTR);////[)//list<blockinfo>::iterator it = getinstance () Blocklist.begin ();//while (it ! = getinstance ()->blocklist.end ())//{//if (it->_ptr == ptr)//{//GetInstance () Blocklist.erase (it);//return;//}//++it;//}//assert (false);//}////static member function does not have this pointer////static function inside cannot call non-static function, no this pointer pass through Static void print ()//{//cout << "Memory leak memory block" << endl;//list< Blockinfo>::iterator it = getinstance ()->blocklist.begin ();//while (it != GetInstance ()->blocklist.end ())//{//printf ("ptr:%p file:%s line:%d\n", it->_ptr, it- >_file.c_str (), it->_line);//++it;//}//}//private://singleton ()//{}//singleton (Const Singleton & s);//singleton& operator= (const singleton& s);//protected://static Singleton* sinstance;//list<blockinfo> blocklist;//};//template<class t>//t *_ _new (Size_t num, const char* file, int line)//{//t *ptr = (T*) ( Singleton::getinstAnce ()->alloc (sizeof (T) *num, file, line));//if (Typetraits <t>::__ispodtype (). Get ())//return ptr;//else//return new (PTR) t;//}//template<class t>//void __delete (T &NBSP;*PTR)//{//if (! Typetraits <t>::__ispodtype (). Get ())//ptr->~t ();//singleton::getinstance ()->dalloc (PTR);//}//template<class t>//t *__ New_array (Size_t num, const char* file, int line)//{//size_t size = sizeof (T) *num;//t* ptr = null;//if (Typetraits <t>::__ispodtype (). Get ())//{//ptr = (t*) (Singleton::getinstance ()->alloc (size, file, line));//return ptr;//}//else//{//size += 4;//ptr = (t*) (Singleton::getinstance ()->Alloc (size, File, line)//* ((int*) PTR) = num;//T* cur = (t*) ((int*) ptr + 1);//for (size_t i = 0; i < num; i++)//{//new (&cur[i]) t;//If the base type is not executed, skip directly over//}//return cur;//}////}//template<class t>//void __delete_array (t* ptr)//{//if (Typetraits <t>::__ispodtype (). Get ())//{//singleton::getinstance ()->dalloc (PTR);//}//else//{//int num = * (int*) ptr - 1);//for (int i = 0; i < num; i++)//{//ptr[i].~T ();//}// Singleton::getinstance ()->dalloc ((void*) ((int*) ptr - 1);//}////}//struct __truetype//{// Bool get ()//{//return true;//}//};////struct __falsetype//{//bool get ()//{//return False;//}//};////template <class _tp>//struct typetraits//{//typedef __falsetype __ispodtype;//};////template <>//struct typetraits< bool>//{// typedef __truetype __ispodtype;//};////template <>//struct Typetraits< char>//{//typedef __truetyPe __ispodtype;//};//template <>//struct typetraits< int >//{//typedef __truetype __ispodtype;//};////#define &NBSP;NEW (type) __new<type> (1,__file__,__line__)//#define &NBSP;DELETE (PTR) __delete (PTR)//#define &NBSP;NEW_ARRAY ( Type,num) __NEW_ARRAY<type> (num,__file__,__line__)//#define &NBSP;DELETE_ARRAY (PTR) __delete_array (PTR)//template type self-deduction type according to passing parameters //singleton* singleton::sinstance = null;//# include<iostream>//using namespace std;//#include "singleton.hpp"////basic type//void test1 ()//{ int *p1 =new (int);//int *p2 =new (int);//int *p3 =new (int);//*p1 = 1 ;//delete (p1);//delete (p2);//delete (p3);//}////custom Type//void test2 ()//{//string *s1 =new (String) ;//*s1 = "ABCD";//cout << s1->c_str () << endl;//string *s2 = new (String);//*s2= "Erty";//cout << s2->c_str () << endl;//delete (S1);//DELETE (s2 );//}////basic type Array//void test3 ()//{//int *p1 = new_array (int, 10);//*p1 = 2;// P1[1] = 3;//p1[9] = 10;//int *p2 = new_array (int, 10);//DELETE_ARRAY ( &NBSP;P1);//delete_array (&NBSP;P2);//}////Custom type Array//void test4 ()//{//string* p1 =new_array (string, 5);//p1[0] = "abc";//p1[1] = "def";//p1[2] = "xxx";//p1[3] = "lll";//p1[4] = "www";//cout << p1->c_str () << endl;// Delete_array (p1);//}//int main ()//{////test4 ();////registers a function to execute after the completion of the main function, the function limit void func (void)//atexit (Singleton::P rint);//system ("pause");//return 0;//}
This article from the "Small Stop" blog, declined to reprint!
Single-Case mode