title: Segmentation fault in stl map<>
body: <p>I am not very good at c++ but I have to test a reputation based system. A code fragment is given below which gives me segfault when i run it on ubuntu system. As i wrote in comments two functions "tackleFirstHandInfo()" and "updateReputation()" individually run correctly but when i call one function from the other it crashes. Any help will be greatly appreciated, thanks in advance. the code is below:</p>
  
  <pre><code>"ex.h"
  
  #ifndef _ex_h
  #define _ex_h
  #include "iostream"
  #include &lt;map&gt;
  #define FADING 0.9
  enum Behaviour {FORWARDING, NOTFORWARDING};
  
  class Rating
  {
  
  private:
      double reputation;
  
  public:
  
      Rating() { reputation = 5.0; }
      Rating(double rep) {reputation = rep;}
  
      ~Rating() {}
  
      double getRep() { return reputation; }
  
      void updateRep(Behaviour behaviour) {
          if (behaviour == FORWARDING)
              reputation = reputation + 1;    
          else 
              reputation = reputation - 1;    
      } 
  
  };
  
  #endif
  
  "ex.cc"
  
  #include &lt;map&gt;                                  
  #include &lt;string&gt;
  #include &lt;iostream&gt;   
  #include "ex.h"                           
  using namespace std;
  typedef map&lt;int, Rating*&gt; ratingTable;
  class RepSys {
  private:
      ratingTable repTable; 
      map&lt;int, Rating*&gt; fHandInfo;
      Rating* rating;
  
  public: 
      RepSys(){}
      ~RepSys(){}
  
      void tackleFirstHandInfo(int address, Behaviour behaviour)
      /* This Function and the function below individually run correctly */
      {
          map&lt;int, Rating*&gt;::iterator it;
          it=fHandInfo.find(address);
          if (it == fHandInfo.end()) {
              cout &lt;&lt; "Adding New Entry for (fHandInfo) "&lt;&lt; address &lt;&lt;endl;
              rating = new Rating();   
              fHandInfo[address] = rating;
          }
          (it-&gt;second)-&gt;updateRep(behaviour);
          cout&lt;&lt;"First Hand Reputation of "&lt;&lt;address&lt;&lt;"\t is ="&lt;&lt; (it-&gt;second)-&gt;getRep()&lt;&lt;endl;
          updateReputation(address, behaviour); // This causes SegFault !!!!
          return;
      }
  
      void updateReputation(int address, Behaviour behaviour)
      {
          map&lt;int, Rating*&gt;::iterator it;
          it = repTable.find(address);
          if (it == repTable.end()) {
              cout &lt;&lt; "Adding New Entry for (repTable) "&lt;&lt; address &lt;&lt;endl;
              rating = new Rating(); 
              repTable[address] = rating;
          }
          (it-&gt;second)-&gt;updateRep(behaviour);
          cout&lt;&lt;"Reputation of "&lt;&lt;address&lt;&lt;"\t is ="&lt;&lt; (it-&gt;second)-&gt;getRep()&lt;&lt;endl;
      }   
  };
  
  int main() {
      int address;    
      RepSys repsys;
      while (address != 0)
      {                                
          cout &lt;&lt; "Address\n";
          cin &gt;&gt; address;
          repsys.tackleFirstHandInfo(address, FORWARDING);
      }
      return 0;
  }
  </code></pre>