分段错误(核心转储) - 访问冲突读取位置x

时间:2016-01-27 20:49:21

标签: c++ arrays

我正在写一个有点玩具程序来进入c ++。但是我在代码中的一个非常特定的行上遇到分段错误时遇到了很多麻烦。我知道分段错误意味着我试图访问未提供给我的程序的memort。 代码(有点)如下(它实际上太大而不能复制粘贴):

class car {
protected:
   int speed;
   std::string brand;
public:
   car (int s, std::string b):speed(s),brand(b) {
       std::cout << "New car has been created" << 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<c/2;i++)
          first[i] = new fast_car (s,b);
      for (int i=c/2;i<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]->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<=rem;i++) 
         if ((cur+1)->is_stopped ()) {   //Exact line where I get the segmentation fault
            tmp = cur->clone ();
            cur++;
         }
   }
};

就是这样。我试图给你尽我所能来重现这个问题。我会尝试解决任何进一步的问题。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

car*new创建,并且不一定是连续的;他们肯定不是你可以用指针算术的数组。 (car*) + 1(car**)[i + 1]不同(更像(car**) + 1)。你可能想要更像(*(first + 1))->doThing(),而不是(first[X] + 1)->doThing()

编辑:要更清楚一点,如果你不能car[X + 1],那么你就不能*(car + 1)。由于car中的clone_fast()是对象指针而不是数组,因此您无法car[X + 1],因此(cur + 1)->is_stopped()是错误的。

答案 1 :(得分:0)

我建议删除所有指针废话,并使用更现代的习语。您可以扩展以下代码。但是,我不清楚为什么你有两个派生类:它们既没有特定的数据也没有方法,为什么呢?

class car
{
private:
    int speed;
    std::string brand;

public:
    car (int s, std::string b):speed(s),brand(b)
    {
        std::cout << "New car has been created" << endl;
    }
    bool is_stopped ()
    {
        return speed==0;
    }
    void speed_up ()
    {
        speed++;
    }
};

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
{
    private:
        std::vector<std::shared_ptr<car>> v_cars;
    public:
        highway (int c, int s, std::string b):number_of_cars(c)
        {
            for (int i=0;i<c/2;i++)
                v_cars.push_back( std::make_shared<fast_car>(s,b) );
            for (int i=c/2;i<c;i++)
                v_cars.push_back( std::make_shared<slow_car>(s,b) );
        }

};

int main()
{
    highway h( 50, 20 );
    return 0;
}