简体   繁体   English

初始化对象时出现分割错误

[英]Segmentation fault at initializing the object

Class vector
{
    int *v ;
    int size;
    public:

    vector(int m ) // create a null vector
    {
        v=new int[size = m];
        for(int i=0;i<size;i++)
            v[i]=0;
    }

    vector(int *a) //create a vector from an array
    {
        for(int i=0;i<size;i++)
            v[i]=a[i];
    }

    int operator*(vector &y) //scalar product
    {
        int sum=0;
        for(int i=0;i<size;i++)
            sum+=this->v[i]*y-v[i];
        return sum;
    }
}; 


int main()
{
    int x[3]={1,2,3};
    int y[3]={4,5,6};
    vector v1(3); //create a null vector of 3 integers
    vector v2(3);
    v1=x; //create v1 from the array x
    v2=y;

    int R=v1*v2;
    cout<<”R=”r;
    return 0;
} 

After executing above program i am getting segmentation fault at this point("v1=x";) Can any one please explain why i am getting segmentation fault. 执行完上面的程序后,我此时遇到分段错误(“ v1 = x”;)可以请任何人解释为什么我遇到分段错误。

When you construct your vector from an int* you don't set the size but you access its value. int*构造vector ,您无需设置size但可以访问其值。 This constructor is used for the implicit conversion from int* to vector needed for the assignment. 此构造函数用于从int*隐式转换为分配所需的vector Accessing an uninitialized value causes the program to have undefined behavior. 访问未初始化的值将导致程序具有未定义的行为。 Most likely the size has some value result effectively in an some inaccessible memory to be accessed. size最有可能具有某些值,从而有效地导致了一些不可访问的内存被访问。

The fact that you have set the left hand side of the assignment to have a specific size doesn't help. 您已将作业的左侧设置为特定大小的事实无济于事。 It could help if you defined your own assignment from int* to vector : 如果您定义自己的从int*vector的赋值, 可能会有所帮助:

vector& vector= (int* other) {
     // ...
}

Having this assignment operator (with a suitable implementation, of course) would avoid the implicit conversion from int* to vector and you could use the size of the left hand side. 使用此赋值运算符(当然,使用合适的实现)可以避免从int*vector的隐式转换,并且您可以使用左侧的size

This is by far not the only program with your code: 到目前为止,这不是唯一带有您的代码的程序:

  • You allocate memory but you never release it. 您分配内存,但从不释放它。 If you allocate memory in a constructor you'll need a destructor to release the memory. 如果在构造函数中分配内存,则需要一个析构函数来释放内存。
  • Of course, once you do you'll easily release memory multiple times because you don't have a copy constructor or a copy assignment and the compiler generated versions do a flat copy. 当然,一旦执行此操作,您将容易地多次释放内存,因为您没有副本构造函数或副本分配,并且编译器生成的版本将进行平面复制。

The problem lies in the way how your memory is allocated. 问题出在您的内存分配方式上。 Let us only look at these four lines: 让我们只看这四行:

vector v1(3); //create a null vector of 3 integers
v1=x; //create v1 from the array x

In the first line, a new object v1 is created by using the vector(int m) constructor form. 在第一行中,使用vector(int m)构造函数形式创建了一个新对象v1。 Here a new int array is allocated on the heap. 这里,在堆上分配了一个新的int数组。

In the third line a new object is created, replacing the one assigned in line 1. And here the problems start, since this line uses the vector(int *a) constructor that does neither allocate the v array, nor does it set the size variable. 在第三行中,创建了一个新对象,替换了在行1中分配的对象。问题开始了,因为此行使用了既不分配v数组也不设置大小的vector(int * a)构造函数变量。 Thus you copy the input to a not allocated address which causes the stack fault. 因此,您将输入复制到未分配的地址,这会导致堆栈错误。 In addition, you never release the memory allocated in the first line's constructor. 此外,您永远不会释放在第一行的构造函数中分配的内存。

Other problems with your code: In the *-operator you multiply an int with a vector 代码的其他问题:在*运算符中,将int与向量相乘

sum += this->v[i] * y - v[i];
                  ^^^

although this multiplication operator is not defined. 尽管未定义此乘法运算符。

This line will also not compile: 该行也不会编译:

cout<<”R=”r;

First because of the missing << between "R=" and r, and then because of the strange ”. 首先是因为“ R =”和r之间缺少<<,然后是因为奇怪。

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

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