title: Segmentation fault (core dumped) - Access violation reading location x
body: <p>I'm writing a somewhat toy program to get into c++. However I'm having quite a lot of trouble with a segmentation fault at a very specific line in my code. I know that the segmentation fault means I tried to access memort that was not given to my program.
  The code is (somewhat) as follows(it's actually too big to just copy paste) :</p>
  
  <pre><code>class car {
  protected:
     int speed;
     std::string brand;
  public:
     car (int s, std::string b):speed(s),brand(b) {
         std::cout &lt;&lt; "New car has been created" &lt;&lt; endl;
     }
     virtual int is_stopped () {
        if (speed==0) return 1;
        else return 0;
     }
     virtual void speed_up () {
         speed++;
     }
     car* clone () {
        car* tmp(this);
        return tmp;
     }
  };
  class fast_car : public car {
  public:
     fast_car (int s, std::string b):car(s,b) { } 
  };
  class slow_car : public car {
  public:
     slow_car (int s, std::string b):car(s,b) { }
  };
  class highway {
     int number_of_cars;
     car **first;           //There are derived classes from car hence the                double pointer
  public:
     highway (int c, int s, std::string b):number_of_cars(c) {
        first = new car*[c];
        for (int i=0;i&lt;c/2;i++)
            first[i] = new fast_car (s,b);
        for (int i=c/2;i&lt;c;i++)
            first[i] = new slow_car (s,b);
     }
     void speed_up () {
        int pos;
        pos = rand()%number_of_cars;    //give me a random number between 0 and number_of_cars;
        if (pos!=0) pos--;   //We don't want position -1
            first[pos]-&gt;speed_up ();
        clone_fast (first[pos], (number_of_cars - pos - 1)); //the second argument is the remaining array "spots" until the end of the array
     }
  
  
  
     void clone_fast (car* cur, int rem) {
        car* tmp;
        for (int i=-;i&lt;=rem;i++) 
           if ((cur+1)-&gt;is_stopped ()) {   //Exact line where I get the segmentation fault
              tmp = cur-&gt;clone ();
              cur++;
           }
     }
  };
  </code></pre>
  
  <p>So there it is. I tried to give you everything I could to recreate the problem. I'll try to solve any further questions.
  Any help would be greatly appreciated.</p>