Seg faults and bad access error with dynamically allocated array of pointers

I don't understand why I'm receiving seg faults and bad access errors with this program. Specs are to use a dynamically allocated array of pointers. Program functions correctly with a static pointer, but I'm running into issues implementing the dynamic memory.

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Person
{
private: 
  Address* addrs;
  //etc
  
public:
  Person(string, string, int, int);
  Person();
  Person(const Person &p);
  Person& operator =(const Person &RHS);
};

Person::Person()                                //default
{
    firstName = "John";
    lastName = "Doe";
    age = -1;
    telNum = -1;
    
    numAddrs = 0;               //current number of addresses for this person

    addrs = *new Address*[10](); //new dynamic array of pointers, initialize to NULL

}

Person::Person(string first, string last, int y, int t)
{                                               //constructor to initialize Person
    firstName = first;
    lastName = last;
    age = y;
    telNum = t;
    
    numAddrs = 0;                   //current number of addresses for this person

    addrs = *new Address*[10](); //new dynamic array of pointers, initialize to NULL

}


Person::Person(const Person &p)     //copy constructor
{
    this->firstName = p.firstName;
    this->lastName = p.lastName;
    this->age = p.age;
    this->telNum = p.telNum;
    this->numAddrs = p.numAddrs;

    
    delete [] addrs;        //delete previous dynamically allocated memory
    
    addrs = *new Address*[10](); //new dynamic array of pointers, initialize to NULL
 
    for (int i = 0; i<10; i++)    //copy addresses to new object
    {
        this->addrs[i] = p.addrs[i];
    }

    
}

Person::~Person()
{
            delete [] addrs;          //dynamically allocated memory
}

erson& Person::operator =(const Person &RHS)
{
    
    this->setFirstName(RHS.firstName);
    this->setLastName(RHS.lastName);
    this->setAge(RHS.age);
    this->setTelNum(RHS.telNum);
    this->setAddress(RHS.addrs);
    this->setNumAddrs(RHS.numAddrs);

    delete [] addrs;
    
    addrs = *new Address*[10](); //new dynamic array of pointers, initialize to NULL

    for (int i = 0; i<10; i++)    //assign addresses to new array
    {
        this->addrs[i]=RHS.addrs[i];
    }
    
    return *this;
}
Last edited on
Lines 23 and 36 result in a memory leak. There is no way to recover the original pointer(s) returned by new, and using delete on the pointer you have will result in undefined behavior. Either change the type of Address::addrs to something appropriate to directly assign the result of your call to new or change the call to new so that it's result may be directly assigned to Address::addrs. Presumably, your Address object doesn't have constructors for Person -- that code is just a little bit screwed up.
Topic archived. No new replies allowed.