tags:

views:

100

answers:

4

I have the following:

class Person
{
public:
  char Name[20];
  char Surname[20];
  char Address[256];


};


Person myperson;

myperson.Name = "Andy";


Person * p_person = new Person();

AlterPersonFunction(p_person);

//Coming out of AlterPersonFunction myperson now contains values of p_pers???


void AlterPersonFunction(Person *p_pers)
{

    Person newperson;

    newperson.Surname = "Chester";

    newperson.Address = "Cookstreet";

    p_pers = &newperson


}

How does that make any sense? I never assigned the pointer to the myperson variable.

EDIT: I will repost with proper code later. I was trying to simplify a scenario I had, but it seems to have failed miserably. Please close this question!!

+1  A: 

You should have used *p_pers = newperson, not p_pers = &newperson. In addition, you should be using std::strings, not fixed-size char[]. In addition, I think you have busted your heap or stack somewhere else which is the cause of this fault (and if you have been using fixed-size char[], it's no surprise). As it is in your OP, your code is buggy to say the least but not in that way.

DeadMG
+2  A: 

AlterPersonFunction won't result in any changes to the Person whose pointer is passed in because of the assignment p_pers = & newperson (which is missing a semicolon). *p_pers = newperson; would work better.

sblom
+1  A: 

p_person's target memory is abandoned without deleting it, which cause a memory leak.

That's not recommanded C++ coding routine, that code is very dangerous

shader
No, it won't leak. He's assigning the address of a stack object to a stack allocated `Person*`, upon returning from `AlterPersonFunction`, `p_person` will not have changed at all. For that to happen, he'd have to pass a `Person**`.
sblom
You are right, change of `Person *p_pers` won't affect original object, because the pointer is passed by value, let me modify my post
shader
A: 

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.

John Rocha
Yeah--none of the string assignments will work as written. He needs to use `strcpy` (or even better something safer like `strlcpy`), or ideally `std::string`. `std::string` would enable string assignment such as all those written above.
sblom