if two pointer variables point to the same memory location, what happens when one of the pointers is freed?
Printable View
if two pointer variables point to the same memory location, what happens when one of the pointers is freed?
Undefined behavior if you try to use the other one.
It frees the memory and then you're left with 2 dangling pointers instead of just 1.Quote:
Originally Posted by snugglebuns
It's not the pointer that is freed. It is the memory that the pointer is pointing to that is freed. In this case, two pointers are pointing to it.
It's like two men standing, pointing at a woman and then all of a sudden lighting strikes her and she's dead. You are left with two men pointing at nothing.
No, then you have two men pointing at a smoking corpse...
Quzah.
Or just her teeth... she could have been blown sky high and landed in a nearby village.
"Hey Bob, look at that woman over there, she's smokin' hot!"
"Who that one? She's not so..."
*FLASH*
"Oh, wow... I guess I see it now..."
p.s. is there any function to manually free memory on the stack?
(Naturally, I tried free... that was a learning experience :D)
Methinks there is, but you'd have to write your function (or at least part of it) in inline assembly.
Or a pair of physics geeks saying, oh wow, check out that cool lightning.
> p.s. is there any function to manually free memory on the stack?
No, the compiler allocates and frees all automatic variables for you.