#include "Stack.h" #include using namespace std; static void nofree(void*) { } /* * This is the client code for the stack. */ int main() { Stack s(nofree); /* Similar to the homework, we're using a little hack here to avoid allocating cells for the * integers, but taking advantage of the fact that integers are the same size as void pointers. */ s.push((void*)1); s.push((void*)2); s.push((void*)3); s.push((void*)4); /* * Remember from section that before we disabled the copy constructor and assignment operator * on the Stack class, the default instantiations the C++ compiler created did the wrong thing; * they only did a shallow copy of the Stack s, which created two stacks pointing to the same * StackNodes. Then when we popped everything off s, s2 was left pointing at deallocated * memory, and we got garbage output for the second loop. * * You can reproduce that bad output by uncommenting code below, and commenting out the lines * in Stack.h that disable the copy constructor and assignment operator. * * Note that because we chose not to add any sort of function for deeply-copying the payloads, we * there is no way to properly implement the copy constructor or assignment operator without * exposing a fairly memory-unsafe contract to Stack clients. */ //Stack s2 = s; //Stack s2(nofree); //s2 = s; //s2.push((void*)5); int out; while (s.pop((void**)&out)) { cout << out << endl; } //while (s2.pop((void**)&out)) { // cout << out << endl; //} return 0; }