title: Object values altered after return from function
body: <p>PART OF A HOMEWORK PROBLEM</p>
  
  <p>I have a list of objects, and my goal is to try and find if an object X is present in that list (I am interested only in the first occurrence of that object). My code seems to work fine for the most part, but I have this strange error where the value of only 1 particular object is being modified after it is returned from a function.</p>
  
  <p>I added 10 objects to the list with values 0 through 3. When I search for any number except 0, (./a.out 1 OR ./a.out 2 and so on)I get the right output. But when I search for 0(./a.out 0), the findInList() prints the correct result, but the main() prints the value 18 (which is not even present in the list).</p>
  
  <p>I am attaching the full source code here in case someone wants to compile it and try it out. I am also attaching the gdb step through I did.</p>
  
  <p>SOURCE:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;functional&gt;
  #include &lt;list&gt;
  using namespace std;
  
  class Page {
  public:
      int pgnum; // page number
      union {
          int lfu_count;
          int lru_clock:1; // can only be 0/1
          int lru_ref8:8;  // we only need 8 bits
      };
  
  public:
      // Constructors
      Page(int num)       { pgnum = num; }
      Page()              {}
  
      // Operator overloading
      bool operator== (const Page &amp;p) const { 
          if(p.pgnum == pgnum)
              return true;
          else
              return false;
      }
      bool operator!= (const Page &amp;p) const { 
          return !(p==*this);
      }
  };
  
  ostream &amp; operator&lt;&lt;(ostream &amp; os, const Page &amp;p) {
      os &lt;&lt; "Page number: " &lt;&lt; p.pgnum;
      return os;
  }
  
  // Think of this as an equivalent to equals in Java (IT IS NOT, JUST IMAGINE)
  struct PageNumber: public binary_function&lt; Page, Page, bool &gt; {
      bool operator () ( const Page &amp;p1, const Page &amp;p2 ) const {
          return p1 == p2;
      }
  };
  
  // Function to find an object in any list given an Operation
  template &lt;class Operation, class T&gt;
  T* findInList( list&lt;T&gt; fullList, T obj, const Operation &amp;op ) {
      T* ret = NULL;
      typename list&lt;T&gt;::iterator it = fullList.begin();
      it = find_if( it, fullList.end(), bind2nd( op, obj ) );
      if( it != fullList.end() ) {
          cout &lt;&lt; "Found obj in list: " &lt;&lt; *it &lt;&lt; endl;
          ret = &amp;(*it); // not the same as it (which is of type iterator)
      }
      return ret;
  }
  
  int main( int argc, char **argv ) {
      Page page_to_find;
      list&lt;Page&gt; frames;
  
      if( argc != 2 ) {
          cout &lt;&lt; "Please enter 1 and only 1 argument" &lt;&lt; endl;
          exit(-1);
      }
      page_to_find.pgnum = atoi(argv[1]);
  
      Page *p = new Page[10];
      for( int i=0; i&lt;10; i++ ) {
          p[i].pgnum = i%4;
          frames.push_back(p[i]);
      }
  
      list&lt;Page&gt;::iterator it_frames = frames.begin();
      while( it_frames != frames.end() ) {
          cout &lt;&lt; "Page in frames: " &lt;&lt; *it_frames &lt;&lt; endl;
          it_frames++;
      }
  
      Page* pg = findInList( frames, page_to_find, PageNumber() );
      if( pg != NULL )
          cout &lt;&lt; "Found page: " &lt;&lt; *pg &lt;&lt; endl;
  
      delete[] p;
  
      return 0;
  }
  </code></pre>