Segmentation Fault Problem
Jun 17, 2015 at 10:31pm UTC
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;
}
Jun 17, 2015 at 10:53pm UTC
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.
Jun 17, 2015 at 11:12pm UTC
It's not supposed to fill listOfNumbers, I was trying to fill up the listOfEmergency.
Jun 17, 2015 at 11:21pm UTC
But you are accessing listOfNumbers which is empty . How is getEmergencyContact()
works? What does it returns?
Jun 17, 2015 at 11:26pm UTC
Remember, theres only 1 reason why you get segmentation fault, Accessing invalid adress
Jun 17, 2015 at 11:31pm UTC
@MiiNiPaa
1 2 3
Contact* Contact::getEmergencyContact() const {
return emergencyContact;
}
Jun 17, 2015 at 11:34pm UTC
Okay, when in your code does that emergencyContact assigned a something meaningful? Remember: by default this address contains garbage.
Jun 17, 2015 at 11:50pm UTC
Every contact has a emergency contact which in of itself is anything contact object
Jun 18, 2015 at 12:04am UTC
Let get it simple:
1 2
Contact c;
c.getEmergencyContact();
What
exactly will return getEmergencyContact here? Why? Show code proving it.
Jun 18, 2015 at 1:14am UTC
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;
}
Jun 18, 2015 at 3:59am UTC
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.