0

I am trying to calculate the partial derivatives of the function f(x,y)=x+y w.r.t x and y at the point (3, 2). I wrote the code but it is giving me an error called Segmentation fault (core dumped). I don't what that is. Can anyone please point out what is wrong in this?

#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

class Der{

private:
    double f;  // function value at x
    vector <double> df; // derivative of function at x
    
public:
    Der();
    Der(vector <double>* v);
    Der operator+(Der); // f + g
    
    void print();
    
};

Der :: Der(){}

Der :: Der(vector <double>* v){
    this->f = v->at(0);
    for(int i=1; i < v->size(); i++){
        this -> df[i-1] = v->at(i);
    }
}


Der Der :: operator+(Der g){
    Der h;
    h.f = this->f + g.f;
    for(int i=0; i< df.size(); i++){
        h.df[i] = this->df[i] + g.df[i];
    }
    return h;
}

void Der :: print(){
    cout<<"Function value at given point is : "<<f<<endl;
    for(int i=0; i< df.size(); i++){
    cout<<"Derivative at given point is : "<<df[i]<<endl;
    }
}

int main()
{
    Der f;
    vector <double> t {3,1,0};
    vector <double> k {2,0,1};
    Der x(&t),y(&k);
    
    f = x+y;
    f.print();
}

Thank you very much!

uuuuuuuuuu
  • 121
  • 5

1 Answers1

1

In your constructor your vector df is empty but you attempt to put values into the vector using

this -> df[i-1] = v->at(i);

This will result in undefined behavior because you are accessing outside the bounds of the empty vector. One way to fix is to replace that line with this:

df.push_back(v->at(i));

which will increase the size of the vector at each call.

Also in Der Der :: operator+(Der g){ you have:

h.df[i] = this->df[i] + g.df[i];

has the same bug as the constructor. h.df is empty. You can fix that with:

h.df.push_back(this->df[i] + g.df[i]);

Also like @πάντα ῥεῖ mentioned in c++ we prefer passing by const reference over a pointer:

 Der :: Der(const vector <double>& v){ 
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • But I don't know I am getting the same error. Is there anything wrong other than this? – uuuuuuuuuu Oct 20 '20 at 18:57
  • Same problem with this line: `h.df[i] = this->df[i] + g.df[i];` – drescherjm Oct 20 '20 at 19:00
  • Ok, I see. Let me correct all the errors. Thank you. – uuuuuuuuuu Oct 20 '20 at 19:00
  • Instead of push_back() before the loops you could use `std::vector::resize()` as an alternate solution: [http://www.cplusplus.com/reference/vector/vector/resize/](http://www.cplusplus.com/reference/vector/vector/resize/) – drescherjm Oct 20 '20 at 19:05
  • So, in the for loop declaration `for(int i=0; i< df.size(); i++){` size of `df` is 0. So, this statement is also invalid. right? – uuuuuuuuuu Oct 20 '20 at 19:07
  • No. In that case the this->df has a size. h.df is what is empty. You have `Der h;` which constructs `h` and after that. `h.df` is empty but you attempt to access elements of `h.df` – drescherjm Oct 20 '20 at 19:09
  • I have already edited the question with the additional fix. – drescherjm Oct 20 '20 at 19:11
  • Oh sorry, yes, yes. You are right. We already stored the values in `df` through the constructor. – uuuuuuuuuu Oct 20 '20 at 19:11