
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cstring>

using namespace std;

struct thing
{
  char buffer[1000];
};

typedef vector<int> vint;
typedef vector<char*> vchar;
typedef vector<thing*> vthing;

// singleton test --------------------------------------

struct One
{
  static One* instance();
  vint data;
};

One* One::instance()
{
  static One n;
  return &n;
}

extern "C" void addOne(int x)
{
  One::instance()->data.push_back(x);
}

// --------------------------------------

vint f1(vint v, int num)
{
  if(num==0) return v;
  v.push_back(num);
  return f1(v,num-1);
}

char* f2(int size)
{
  return new char[size];
}

void f3(char* arr)
{
  delete [] arr;
}

thing* f4()
{
  return new thing;
}

void f5(thing* a)
{
  if( (unsigned int)rand() < (RAND_MAX/80))
    {
      cerr << "leaked " << (void*)a << endl;
      return;
    }
  delete a;
}

void callf5(thing* a) { f5(a); }
void callf3(char* a) { f3(a); }

short* glob_f = 0;

void f3_leak()
{
	char* b = new char[101];
	short* f = new short(4);
	char* w = strdup("ji");
	glob_f = f;
	delete b;
}
void f2_leak() { f3_leak(); }
void f1_leak() { f2_leak(); }

int main()
{
	f1_leak();
	return 0;
  vint a;
  vint v = f1(a,20);
  int i,j;

  for(i=0;i<100;++i)
    for(j=0;j<10;++j)
    {
      f3(f2(i));
      f5(f4());
    }

  vchar c;
  vthing t;

  for(i=0;i<100;++i)
    for(j=0;j<10;++j)
    {
      c.push_back(f2(i));
      t.push_back(f4());
    }

  for_each(c.begin(),c.end(),callf3);
  for_each(t.begin(),t.end(),callf5);

  const vint& d = One::instance()->data;
  copy(d.begin(),d.end(),ostream_iterator<int>(cout,"\n"));

  return 0;
}

