This may be stack corruption issue? I'm focusing on your AlterPersonFunction
// pass in a person pointer (p_pers)
void AlterPersonFunction(Person *p_pers)
{
// declare a stack object, newperson
//
Person newperson;
// Set the new persons surname and address
//
newperson.Surname = "Chester";
newperson.Address = "Cookstreet";
// Change the p_pers pointer to now point at the local variable.
//
p_pers = &newperson;
}
I don't think this will cause anything to change. p_pers's will be changed to point at the newperson stack variable, but that change will not be propagated out of the function, since its not a pointer reference, and its not a pointer to a pointer.
And if it did get propagated back, you'd have an even worse case because now you would have dropped the dynamically allocated memory on the floor, and changed the p_pers pointer to point at a stack variable that pops off the stack when the function returns.
If you want to get the values from newperson into the p_pers pointer you would need to do something like:
*p_pers = newperson
for the last line.
Alternatively, you could change the function to something like:
void AlterPersonFunction (Person *p_pers)
{
p_pers->Surname = "Chester";
p_pers->Address = "Cookstreet";
}
As an aside I tried to compile this locally to look into this, but I get compile time erors when you do:
myperson.Name = "Andy";
the compiler complains:
error: incompatible types in assignment of 'const char [8]' to 'char [20]'
Does this compile? If so which compiler are you using? g++ doesn't like it.