/* Using pointers in various ways. No real application */
 
#include <string>
#include <iostream>
 
using namespace std;
 
int* maker (int size) {
	int *newstuff = new int[size];
	for (int i = 0; i< size; i++) {
		newstuff[i] = i*2;
	}
	return newstuff;
}
 
//EVIL EVIL EVIL DON'T *EVER* do this
int* evilmaker () {
	const int SIZE = 10;
	int stuff[SIZE];
	for (int i = 0; i< SIZE; i++) {
		stuff[i] = i*2;
	}
	return stuff;
}
 
int main () {
	int friends_at_home = 2;
	int friends_at_work = 3;
	int a[] ={1,2,3};
	int* friends_where_I_am;
 
	cout << "Beginning ...\n\n";
 
	friends_where_I_am = &friends_at_home;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at friends_at_home the value is: " << *friends_where_I_am << endl;
 
	cout << "change friends_where_I_am\n";
	friends_where_I_am = &friends_at_work;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at friends_at_work the value is: " << *friends_where_I_am << endl;
 
	cout << "\nchange the number pointed to by friends_where_I_am\n\n";
	*friends_where_I_am = 4;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at friends_at_work the value is: " << *friends_where_I_am << endl;
	cout << "friends_at_work is: " << friends_at_work << endl;
 
	cout << "\nArrays are different\n\n";
 
	cout << "change friends_where_I_am to point at an array\n";
	friends_where_I_am = a;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at array the value is: " << *friends_where_I_am << endl;
	friends_where_I_am++;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at array the value is: " << *friends_where_I_am << endl;
 
	cout << "\nI'll manage my own memory, thank you!\n\n";
 
	cout << "\nI'll manage my own memory, thank you!, first with integers:\n\n";
 
	cout << "Get some memory\n";
	friends_where_I_am = new int;
	cout << "Set int...\n";
	*friends_where_I_am = 42;
 
 
	cout << "...and print it out\n";
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "the value there is: " << *friends_where_I_am << endl;
 
	cout << "Now delete the pointer.\n";
	delete friends_where_I_am;
 
	cout << "Begin of BAD BAD BAD!!! This is a dangling pointer!!\n";
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "but stuff is still there?? " << *friends_where_I_am << endl;
	friends_where_I_am++;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at array the value is: " << *friends_where_I_am << endl;
	cout << "END of bad dangling pointer!!\n";
 
	cout << "\nI'll manage my own memory, thank you!, now with arrays:\n\n";
 
	cout << "Get some memory, first, I need to kow how much?";
	int mysize;
	cin >> mysize;
	friends_where_I_am = new int[mysize];
	cout << "fill the array with stuff...\n";
	for (int i = 0; i< mysize; i++) {
		friends_where_I_am[i] = i*2;
	}
 
	cout << "(friends_where_I_am is: " << friends_where_I_am << ")\n";
 
	cout << "...and print it out\n";
	for (int i = 0; i< mysize; i++) {
		cout << friends_where_I_am[i] << endl;
	}
 
	cout << "Now delete the pointer.\n";
	delete[] friends_where_I_am;
 
	cout << "Begin BAD BAD BAD!!! This is a dangling pointer!!\n";
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "but stuff is still there?? " << friends_where_I_am[1] << endl;
	// Note that I treated it as an array on the previous line as as a pointer on the next line.
	// There are just different ways of saying the same thing
	friends_where_I_am++;
 
	cout << "friends_where_I_am is: " << friends_where_I_am << endl;
	cout << "when pointer is pointing at array the value is: " << *friends_where_I_am << endl;
	cout << "END of bad dangling pointer!!\n";
 
	cout << "\nNow using a function!!\n\n";
	mysize = 4;
	friends_where_I_am = maker(mysize);
	for (int i = 0; i< mysize; i++) {
		cout << friends_where_I_am[i] << endl;
	}
 
	cout << "Some one should free the memory, I guess I will.\n";
	delete[] friends_where_I_am;
 
	cout << "\nNow using a VERY VERY EVIL function!!\n\n";
	friends_where_I_am = evilmaker();
	for (int i = 0; i< 10; i++) {
		cout << friends_where_I_am[i] << endl;
	}
 
	cout << "\n...end\n";
	system("pause");
	return 0;
}
cs-142/fun-with-pointers.txt · Last modified: 2014/11/04 15:26 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0