When you are inside a function that receives a pointer argument and you need to change the address in the pointer, then you need to delete the memory at the address in the pointer, allocte your new stuff and put the new address in the pointer.
You can't tell from a pointer if:
a) the address in the pointer is to a variable on the stack. If it is and you delete, you crash.
b) the address in the pointer is to a varible on the heap. If it is and you delete, then any copies of that pointer (like in the calling function) are invalid. When you use them, you crash.
So the only safe way to delete a pointer is a) it points to a heap variable and b) you have the last copy of that pointer.
That means, in C, you have to manually keep track of the number of copies of a given pointer. That is, when you call a function, you increment the count. As you leave the function, you decrement the count.The combination of the pointer and the count is called a handle. These would be in a struct.
The count is called a handle (or reference) count.
In C++, you use a handle class:
http://bytes.com/forum/thread651599.html.