Memory access problem with pointer

Jul 2, 2015 at 8:10pm
I'm having a run-time error with memory access. I tried to shorten my code below to highlight what's important, hopefully I didn't take out what's useful to analyse the problem.

Debug

For some reason I'm getting segmentation fault. I searched up the reasoning and it has to do with accessing memory that I don't have access to. However, the statement std::cout << d1.getPatientNameAtPosition(2) << "\n"; is simply accessing the pointerArray in the private member of the Doctor.

Could anyone see what's wrong with my code that causes this problem?
Last edited on Jul 2, 2015 at 11:52pm
Jul 2, 2015 at 10:40pm
I think that what's happening is that my pointer array ceases to exist when I reach the function d1.getPatientNameAtPosition
Jul 2, 2015 at 11:05pm
No, I don't think that's it.

Post the code for Patient.
Jul 2, 2015 at 11:33pm
You were right that wasn't it. It was because of my destructor and I didn't include it in my code. My mistake! XD
It basically looked like this

1
2
3
 ~Doctor::Doctor{
delete[] patientArray;
} 


This would end up for some reason deleting my pointerArray beforemy function getPatientNameAtPosition ran.


EDIT: The error did not reside in the Destructor but actually in my addPatient function. My addPatient function was if (maxArraySize < 20 ). But maxArraySize is 20 therefore the if condition is never fulfilled and the Array is never written.
Last edited on Jul 2, 2015 at 11:53pm
Topic archived. No new replies allowed.