Advertisement
Aslai

Reference Counter With Example

Feb 10th, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include<cstdio>
  2.  
  3. namespace Memory{
  4.     template<class type>
  5.     class counter{
  6.         type** myptr;
  7.         unsigned int *count;
  8.  
  9.         void countpp(){
  10.             if( count == 0 ) return;
  11.             (*count) ++;
  12.         }
  13.         void countss(){
  14.             if( count == 0 ) return;
  15.             (*count)--;
  16.             if( (*count) == 0 ){
  17.                 delete count;
  18.                 delete *myptr;
  19.                 delete myptr;
  20.             }
  21.         }
  22.         public:
  23.         counter(){
  24.             myptr = 0;
  25.             count = 0;
  26.         }
  27.         counter(type *t){
  28.             myptr = new type*;
  29.             count = new unsigned int;
  30.             *myptr = t;
  31.             (*count) = 1;
  32.         }
  33.         counter( const counter<type> &t ){
  34.             myptr = t.myptr;
  35.             count = t.count;
  36.             countpp();
  37.         }
  38.         ~counter(){
  39.             countss();
  40.         }
  41.         type*& get(){
  42.             return *myptr;
  43.         }
  44.         type*& operator->(){
  45.             return *myptr;
  46.         }
  47.         type& operator*(){
  48.             return **myptr;
  49.         }
  50.         type& operator[](int idx){
  51.             return (*myptr)[idx];
  52.         }
  53.         counter<type>& operator=(counter<type> & t){
  54.             countss();
  55.             myptr = t.myptr;
  56.             count = t.count;
  57.             countpp();
  58.             return *this;
  59.         }
  60.  
  61.     };
  62. }
  63.  
  64. struct test{
  65.     int garbage[2500000]; //10MB
  66. };
  67.  
  68. typedef Memory::counter<test> TestStruct;
  69. typedef Memory::counter<int> TestInt;
  70.  
  71.  
  72. int main(){
  73.     {
  74.         TestStruct passback;
  75.         {
  76.             TestStruct bigval(new test);
  77.             for( int i = 0; i < 2500000; ++i )
  78.                 bigval->garbage[i] = i;
  79.             passback = bigval;
  80.         }
  81.         printf("%d\n", passback->garbage[533646] );
  82.         getchar();
  83.     }
  84.     printf("The memory should be freed now.\n");
  85.     getchar();
  86.  
  87.     {
  88.         TestInt passback;
  89.         {
  90.             TestInt bigval(new int[25000000]); //100MB
  91.             for( int i = 0; i < 25000000; ++i )
  92.                 bigval[i] = i;
  93.             passback = bigval;
  94.         }
  95.         printf("%d\n", passback[13322646] );
  96.         getchar();
  97.     }
  98.     printf("The memory should be freed now.\n");
  99.     getchar();
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement