Segfault on deep copy

Hi I've got to write a class called course that makes two objects and has a function that one time has a default value when called and another time sets a value to a grade value. I got it to compile and it uselessly works with shallow copy but why is it segfaulting when I try and add a deep copy constructor to stop the values messing up, Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  #include<iostream>

using namespace std;

class Course
{
public:
Course(){};
Course(Course const &);
~Course(){}
int SetPassingGrade(int itsp=5)const{itsp = *PassingGrade;}
int SetPassingGrade(int p){p = *PassingGrade;}
int GetPassingGrade()const{return *PassingGrade;}
private:
int *PassingGrade;
};


Course::Course(const Course & rhs)
{
PassingGrade = new int;
*PassingGrade = rhs.GetPassingGrade();
}



int main(){
Course PaperOne;
Course PaperTwo;
cout << "Setting grades's\n";
PaperOne.SetPassingGrade();
PaperTwo.SetPassingGrade(10);
cout << "The value of PaperOne's grade is : " << PaperOne.GetPassingGrade() << "\n";
cout << "The value of PaperTwo's grade is : " << PaperTwo.GetPassingGrade() << "\n";
}
PaperOne.SetPassingGrade();

The function SetPassingGrade attempts to dereference the pointer PassingGrade. In PaperOne, that pointer was never set to point at anything, so you're trying to dereference random memory. This is bad. This segFaults.

Also, your functions SetPassingGrade don't set anything.
Is it necessary to use a pointer?
With a simple int you could avoid dtor, copy ctor and all other problems.
Hi if I did that how could I stop the copied values messing up. Best wishes. Alistair.
Also guys I thought passingGrade is pointing to a location on the freestore please explain, should i try and put that in private instead of the ctor. Thanks.
Like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

class Course {
  public:
    Course() {};
    void SetPassingGrade(int itsp = 5)
    {
        PassingGrade = itsp;
    }
    int GetPassingGrade() const
    {
        return PassingGrade;
    }
  private:
    int PassingGrade;
};


int main(){
    using std::cout;
    
    Course PaperOne;
    Course PaperTwo;
    cout << "Setting grades\n";
    PaperOne.SetPassingGrade();
    PaperTwo.SetPassingGrade(10);
    cout << "The value of PaperOne's grade is : " << PaperOne.GetPassingGrade() << "\n";
    cout << "The value of PaperTwo's grade is : " << PaperTwo.GetPassingGrade() << "\n";
}
Last edited on
Is it even possible to have a function set a default value deep copy the object and then overload the function to take a new value or am I trying to do something impossible and the tutor just wanted me to use one object so no deep copy or pointers?
You should always avoid pointers when you can. You should always avoid dynamic memory when you can. Did the assignment specifically say to use pointers and write deep copies? If the assignment was just
write a class called course that makes two objects and has a function that one time has a default value when called and another time sets a value to a grade value
then you don't need pointers and you don't need to write your own copy constructors.
Topic archived. No new replies allowed.