Segmentation Fault Problem

I've tried many different variations of code for this function and I was wondering if someone could help me understand why I am getting a segmentation fault error.

Most recent attempt:
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
void PhoneBook::verifyAllContacts(){
	Contact* listOfNumbers = new Contact[numContacts];
	Contact* listOfEmergency = new Contact[numContacts];
	
    int tempHoldCount = 0;
	for(int x = 0;  x < numContacts; x++){
		Contact* temp = new Contact;
		temp = listOfNumbers[x].getEmergencyContact();
		listOfEmergency[x] = *temp;
	}
	
    for(int i = 0; i < numContacts; i++) {
        if(listOfNumbers[i].verifyPhoneNumber() == true && listOfEmergency[i].verifyPhoneNumber() == true)
            tempHoldCount++;
    }
	Contact* validContactList = new Contact[numContacts];
    
	int hold = 0; 

	for(int z = 0; z < numContacts; z++){
		if(listOfNumbers[z].verifyPhoneNumber() == true && listOfEmergency[z].verifyPhoneNumber() == true){
		Contact* tempTwo = new Contact;
		*tempTwo = listOfNumbers[z];
		validContactList[hold] = *tempTwo;
		hold++;
		}
		else{
			break;
		}
	}


Past attempt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void PhoneBook::verifyAllContacts(){
 Contact* listOfNumbers;
    listOfNumbers = new Contact[numContacts];
    int tempHoldCount = 0;

    for(int i = 0; i < numContacts; i++) {
        if(listOfNumbers[i]->verifyPhoneNumber() == true && listOfNumbers[i].getEmergencyContact()->verifyPhoneNumber() == true)
            tempHoldCount++;
    }

    Contact* validContactList;
    validContactList = new Contact[tempHoldCount];
	int hold = 0; 

	for(int z = 0; z < numContacts; z++){
		if(listOfNumbers[z]->verifyPhoneNumber() == true && listOfNumbers[z].getEmergencyContact()->verifyPhoneNumber() == true){
		validContactList[hold] = listOfNumbers[z];
		hold++;
		}
	}
    delete [] listOfNumbers; 
}
1
2
3
Contact* listOfNumbers = new Contact[numContacts];
//...
temp = listOfNumbers[x].getEmergencyContact();
Between those two lines you do not change or fill listOfNumbers. So it is default constructed and it is possible that getEmergencyContact returns garbage.
It's not supposed to fill listOfNumbers, I was trying to fill up the listOfEmergency.
But you are accessing listOfNumbers which is empty. How is getEmergencyContact() works? What does it returns?
Remember, theres only 1 reason why you get segmentation fault, Accessing invalid adress
@MiiNiPaa
1
2
3
Contact* Contact::getEmergencyContact() const{
	return emergencyContact;
}
Okay, when in your code does that emergencyContact assigned a something meaningful? Remember: by default this address contains garbage.
Every contact has a emergency contact which in of itself is anything contact object
Let get it simple:
1
2
Contact c;
c.getEmergencyContact();
What exactly will return getEmergencyContact here? Why? Show code proving it.
It prints out a Contact.
I came up with another code but same error:
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
void PhoneBook::verifyAllContacts(){

        Contact* listOfNumbers = new Contact[numContacts];
       
        int counter = 0;
        for(int i = 0; i < numContacts; i++){
                if(listOfNumbers[i].verifyPhoneNumber() == false){
               
 
                }
                else if(listOfNumbers[i].verifyPhoneNumber() == true && listOfNumbers[i].getEmergencyContact()->verifyPhoneNumber() == true){
                        counter++;
                }
                else if(listOfNumbers[i].verifyPhoneNumber() == true && listOfNumbers[i].getEmergencyContact() == NULL){
                        counter++;
                }      
        }
        Contact* newListOfNumbers = new Contact[counter];
        for(int x = 0; x < numContacts; x++){
                int hold = 0;
                if(listOfNumbers[x].verifyPhoneNumber() == false){
                       
                }
                else if(listOfNumbers[x].verifyPhoneNumber() == true && listOfNumbers[x].getEmergencyContact()->verifyPhoneNumber() == true){
                        newListOfNumbers[hold] = listOfNumbers[x];
                        hold++;
                }
                else if(listOfNumbers[x].verifyPhoneNumber() == true && listOfNumbers[x].getEmergencyContact() == NULL){
                        newListOfNumbers[hold] = listOfNumbers[x];
                        hold++;
                }
        }
        delete [] listOfNumbers;
}
It prints out a Contact
Which Contact? Where it miraculously sprout into existence in progam, seeing an no other contacts were created in a program.

Show a default constructor to prove that default constructed Contact will have a properly initialize emergency contact. Because most likely cause of error is this.
Topic archived. No new replies allowed.