#include <vector>
#include <iostream>
#include <boost/shared_array.hpp>
#include <cstring>

using namespace std;
using namespace boost;

int main(void)
{
  vector< shared_array<char> > v;

  while(true)
  {
    cout<<"size: ";
    size_t size;
    cin>>size;
    cout<<"(re)allocating to "<<size<<"MB"<<endl;
    size*=1024;

    for(size_t i=v.size(); i<size; ++i)
    {
      shared_array<char> tmp(new char[1024]);
      memset( tmp.get(), 0xF0, 1024 );
      v.push_back(tmp);
    }

    random_shuffle( v.begin(), v.end() );

    if( size<v.size() )
      v.resize(size);

    if( size==0 )
    {
      cout<<"deallocating"<<endl;
      vector< shared_array<char> > tmp;
      v.swap(tmp);
    }

    cout<<"size is "<<v.size()/1024<<"MB"<<endl<<endl;
  }

  return 0;
}
