If a pointer goes out of scope or its parent object is deleted, the object that the pointer references still exists in memory. Thus the rule of thumb that any code that allocates ( news ) an object owns the object and should also delete that object when it's no longer needed.
Your swap() function is simply swapping values. It uses pointers to determine exactly where in memory those values are stored, and the pointers are dereferenced in order to get at the actual values.
The variable number is created inside the pointer() function and should go out of scope after the function has been executed.
I haven't programmed in C++ for a number of years, so I decided to refresh my memories on pointers.
In the classic example of swapping between two numbers, the example is
void swapPointersClassic(double *num1, double *num2)
{
double temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
This allows us to make function call like swapPointersClassic(&foo, &bar);
and because we pass in the memory addresses of both variables foo and bar, the function will retrieve the values and do the swap. But I started to wonder, why can't I do the following?
void swapPointers(double *num1, double *num2)
{
double *temp;
temp = num1;
num1 = num2;
num2 = temp;
}
That seems to make more sense to me, because we only have to create enough temporary storage for storing the memory address num1 (instead of a full temporary storage for storing a double value *num1). However, it seems that function scope limits the effect of pointer swapping. After making the call swapPointers(&foo, &bar);
, I can see that within the function swapPointers, foo & bar are indeed swapped. Once we exit the swapPointers function, foo and bar are no longer swapped. Can anyone help me understand why this is the case? This behavior reminds me of the typical pass by value approach, but we are passing by pointers here. So that means we can only touch the values pointed by those pointers, but not the pointers themselves?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With