/*****************************************************************************
 * 'main' illustrates automatic, static, and dynamic memory.
 * **************************************************************************/
#include<iostream>
#include<iomanip>

using std::cin; using std::cout;
using std::endl; 
/* This function just returns a reference to a static variable storing
 * the number of times the function has been called.
 */
int* visits()
{
	static int x=0;/*This is allocated only once. After the first time 
			*the cpu encounters this declaration, it ignores it.
			*/
	
	x++;
	return &x;
}

int* invalid_pointer()/* This returns a pointer to a value that is 
			 automatically deallocated on return.
			 */
{
	int x=111;
	return &x;//This will produce a warning at compile time.
}

int* valid_pointer()/* The pointer this returns can be deallocated with
		      * "delete". */
{
	int* x=new int(111);
	return x;
}
	

int main()
{
	
	visits();
	visits();
	cout<<"The value of the static x after 3 visits is "<<*visits();
	int* y=invalid_pointer();//We'll get a warning at compile time.
	cout<<endl<<"We assigned y an obsolete pointer to 111, now y holds "
		<<*y;// *y is garbage.
	y=valid_pointer();
	cout<<endl<<"We assigned y dynamically allocated pointer to 111."
	       <<" Now y holds "
		<<*y;
	return 0;
}

	
