Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualBacterius

Posted 01 January 2013 - 10:38 PM

You have a scope problem. The value of your pointer, cur_state, being an argument, only exists within this function, and will be forgotten once you return from it (while any reference it was pointing to, will not). So, the delete instruction will destroy the reference the pointer is pointing to, and the new instruction will correctly create a new instance and make cur_state point to it.

 

However, as soon as you return from the function, the value of the cur_state pointer (which is, the address of the instance you just allocated) will be lost, and the caller will be left with the old value, which was the address of the instance you just deleted a few lines earlier. This is, in essence, because you passed the pointer by value and not by reference. The solution is to pass the pointer by reference, or pass a pointer to the pointer, or return the new pointer instead.

 

Basically, the thing to understand is that you did not pass cur_state to your change_state function, but only its value. Whatever pointer variable you passed as an argument to this function, will be left unchanged, a copy of it will be made, and discarded after the function returns. The unfortunate result being, that the pointer to "new State_Exit()" never made it beyond the "return" statement. As soon as the function returned, the copy holding the new, valid pointer was discarded, and you kept on going with the old value, which now points to a deleted instance.

 

The new State_Exit() object is there, well and alive. You just don't know where it is, and have no reference to it anywhere in your code and no way to access it beyond the function where it was created, because you lost your pointer to it after said function returned.

 

Try making cur_state a reference to a pointer instead, that way you will be affecting the real cur_state variable and not just a copy, and your code should work. You can also use a pointer to a pointer (so that you can now affect the inner pointer outside the function too, as well as the object, which is pointed to by the pointed pointer), but this is more C-ish and more error-prone.


#3Bacterius

Posted 01 January 2013 - 10:33 PM

You have a scope problem. The value of your pointer, cur_state, being an argument, only exists within this function, and will be forgotten once you return from it (while any reference it was pointing to, will not). So, the delete instruction will destroy the reference the pointer is pointing to, and the new instruction will correctly create a new instance and make cur_state point to it.

 

However, as soon as you return from the function, the value of the cur_state pointer (which is, the address of the instance you just allocated) will be lost, and the caller will be left with the old value, which was the address of the instance you just deleted a few lines earlier. This is, in essence, because you passed the pointer by value and not by reference. The solution is to pass the pointer by reference, or pass a pointer to the pointer, or return the new pointer instead.

 

Basically, the thing to understand is that you did not pass cur_state to your change_state function, but only its value. Whatever pointer variable you passed as an argument to this function, will be left unchanged, a copy of it will be made, and discarded after the function returns. The unfortunate result being, that the pointer to "new State_Exit()" never made it beyond the "return" statement. As soon as the function returned, the copy holding the new, valid pointer was discarded, and you kept on going with the old value, which now points to a deleted instance.


#2Bacterius

Posted 01 January 2013 - 10:27 PM

You have a scope problem. The value of your pointer, cur_state, being an argument, only exists within this function, and will be forgotten once you return from it (while any reference it was pointing to, will not). So, the delete instruction will destroy the reference the pointer is pointing to, and the new instruction will correctly create a new instance and make cur_state point to it.

 

However, as soon as you return from the function, the value of the cur_state pointer (which is, the address of the instance you just allocated) will be lost, and the caller will be left with the old value, which was the address of the instance you just deleted a few lines earlier. This is, in essence, because you passed the pointer by value and not by reference. The solution is to pass the pointer by reference, or pass a pointer to the pointer, or return the new pointer instead.


#1Bacterius

Posted 01 January 2013 - 10:27 PM

You have a scope problem. Your pointer, cur_state, being an argument, only exists within this function, and will be forgotten once you return from it (while any reference it was pointing to, will not). So, the delete instruction will destroy the reference the pointer is pointing to, and the new instruction will correctly create a new instance and make cur_state point to it.

 

However, as soon as you return from the function, the value of the cur_state pointer (which is, the address of the instance you just allocated) will be lost, and the caller will be left with the old value, which was the address of the instance you just deleted a few lines earlier. This is, in essence, because you passed the pointer by value and not by reference. The solution is to pass the pointer by reference, or pass a pointer to the pointer, or return the new pointer instead.


PARTNERS