if two pointer variables point to the same memory location, what happens when one of the pointers is freed?
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.Originally Posted by snugglebuns
If you understand what you're doing, you're not learning anything.
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.
Hope is the first step on the road to disappointment.
Or just her teeth... she could have been blown sky high and landed in a nearby village.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
"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 )
Methinks there is, but you'd have to write your function (or at least part of it) in inline assembly.
Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.
- Mike McShaffry
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.