Thread: pointer question

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    1

    pointer question

    if two pointer variables point to the same memory location, what happens when one of the pointers is freed?

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Undefined behavior if you try to use the other one.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by snugglebuns
    if two pointer variables point to the same memory location, what happens when one of the pointers is freed?
    It frees the memory and then you're left with 2 dangling pointers instead of just 1.
    If you understand what you're doing, you're not learning anything.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    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.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, then you have two men pointing at a smoking corpse...


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    "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 )

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM