简体   繁体   English

声明向量时出现分段错误

[英]Segmentation fault when declaring a vector

I'm writing a programme with two classes: 我正在编写一个包含两个类的程序:

 class Sequence {

    protected:
    vector<int> seq_;

    public:
    Sequence(){
            for(int i=0; i<16;i++)
                    seq_.push_back(0);
    };
    Sequence(int a, int b,int c){
            for(int i=0; i<16; i++)
                    seq_.push_back(a*i+b*i*i+c*i*i*i);
    };
    Sequence(const Sequence & myseq){
            for(int i=0; i<16;i++)
                    seq_[i]=myseq.Get_i(i);
    };

    int Get_i(int i)const{
            return seq_[i];
    };

    void Print() const { 
            for(int i=0; i<16; i++)
                    cout<<seq_[i]<<endl;
    };

    int operator*(Sequence & myseq) const {
            int sum=0;
            for(int i=0; i<16;i++)
                    sum+=seq_[i]*myseq.seq_[i];
            return sum;
    };

    void operator=(Sequence & myseq) {
            for(int i=0; i<16; i++)
                    seq_[i]=myseq.seq_[i];
    };

};

This first class is devoted to containing a sequence and overloading some basics operators. 第一类致力于包含序列并重载一些基本运算符。 The following, on the other side, contains a binary sequence corresponding to a number (or a random binary sequence if the default constructor gets the call). 另一方面,以下内容包含对应于数字的二进制序列(如果默认构造函数得到调用,则为随机二进制序列)。

 class Binary : public Sequence {

    private:

    public:
    Binary(){
            for(int i=0; i<16; i++)
                    seq_.push_back(round(drand48()));
    };

    Binary(int num){
            double prec=num; double last=0;
            for(int i=16; i>=0; i--){
                            prec=prec-last;
                            if(int(prec/pow(2,i))==0){
                                    seq_.push_back(0);
                                    last=0;
                            }else{
                                    seq_.push_back(1);
                                    last=pow(2,i);
                            }
            }
    };

    Binary  not_ () {
            Binary mybin;
            for(int i=0; i<16; i++){
                    if( seq_[i]==1){
                            mybin.seq_[i]=0;
                    }else{  
                            mybin.seq_[i]=1;
                    };
            };
            return mybin;
    };

    int cost (Sequence myseq){
            int k=myseq*(*this)-(Binary::not_())*myseq;
            return k;
    };



  };

The problem is that I get a Segmentation Fault just defining a vector: 问题是我只定义一个向量就遇到了细分错误:

 vector<Binary> mybins (pow(2,16));

I ran GDB and it stuck at the copy constructor: 我运行了GDB,它停留在复制构造函数中:

 Sequence(const Sequence & )

I was wondering if you could give me any help to find the error and to explain it to me. 我想知道您是否可以帮助我找到错误并向我解释。 I'm guessing it has something to do with my poor knowledge of how the standard library works! 我猜想这与我对标准库的工作原理了解不足有关! Thank you for your time. 感谢您的时间。

Sequence(const Sequence & myseq){

This is a constructor. 这是一个构造函数。 It is constructing a new object. 它正在构造一个新对象。

        for(int i=0; i<16;i++)
                seq_[i]=myseq.Get_i(i);

The seq_ member is an initially empty vector. seq_成员是最初为空的向量。 Attempting to set nonexistent values in the vector is undefined behavior, and the reason for your crash. 尝试在向量中设置不存在的值是未定义的行为,也是崩溃的原因。 Your other constructors use push_back() , correctly, to insert new values into a vector. 您的其他构造函数正确使用push_back()将新值插入向量中。 This should be done here as well. 这也应该在这里完成。

All your constructors start with an empty seq_ and correctly use push_back to grow it and add elements to it ... except for the copy constructor, which also starts with and empty seq_ but uses [] instead. 您的所有构造函数seq_seq_并正确使用push_back进行增长并向其中添加元素...除了复制构造函数之外,复制构造函数也以seq_开头,但使用[]代替。 So it's accessing elements which are not in the vector, giving you Undefined Behaviour. 因此,它正在访问不在向量中的元素,从而为您提供未定义的行为。

To solve this correctly, remove the copy constructor altogether. 若要正确解决此问题,请完全删除复制构造函数。 The compiler will then generate one for you, which will do just what you need. 然后,编译器将为您生成一个文件,该文件将满足您的需求。 As a bonus, it will also generate a move constructor. 另外,它还会生成一个move构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM