简体   繁体   中英

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.

When you construct your vector from an int* you don't set the size but you access its value. This constructor is used for the implicit conversion from int* to vector needed for the assignment. 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.

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 :

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.

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. Here a new int array is allocated on the heap.

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. 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

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 ”.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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