Thread: pointer state, how to test?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    11

    pointer state, how to test?

    Hello,

    An object passes a pointer as a parameter to a function call.

    Is there a was to find if the memory was freed by the calling function and the pointer is not valid?

    BR,
    Cristian

  2. #2
    Iamregistered
    Guest
    you can test if the pointer is valid by seeing if it is NULL or not. If it is, this does not however mean that the memory was released - the pointer may just have been assigned to a different address.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: pointer state, how to test?

    Originally posted by Cristian Negres
    Hello,

    An object passes a pointer as a parameter to a function call.

    Is there a was to find if the memory was freed by the calling function and the pointer is not valid?

    BR,
    Cristian
    No, that's something you have to keep track of yourself.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: pointer state, how to test?

    Originally posted by Cristian Negres
    Hello,

    An object passes a pointer as a parameter to a function call.

    Is there a was to find if the memory was freed by the calling function and the pointer is not valid?

    BR,
    Cristian
    This is the reason NULL pointers exists. NULL pointer => Whatever it pointed at has been freed/doesn't exist any more.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    11
    Hi,

    Thank you for your help.

    BR,
    Cristian

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Originally posted by Magos
    This is the reason NULL pointers exists. NULL pointer => Whatever it pointed at has been freed/doesn't exist any more.
    Really? I thought that delete only deallocated memory...not change the pointer.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by pianorain
    Really? I thought that delete only deallocated memory...not change the pointer.
    I never said that delete sets the pointer to NULL. That is up to you. You should always set the pointer to NULL after deletion to make sure the adress cannot be accessed or a call to delete again doesn't screw something up.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    The NULL pointer always (to my knowledge... there may be exceptions) points to address 0 in memory. Most operating systems never assign any object there. It's strictly off-limits. A pointer set to this place will never contact a real object.

    Also, any attempt to access this space in memory automatically results in a Seg Fault. If anything attempts to dereference the pointer, the program will tell them immediately, and the program will crash.

    If the pointer wasn't reallocated, the memory the pointer's accessing could be garbage, and the program would still run. This makes finding the error much harder.

  9. #9
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Unfortunately, the instance of the pointer that is deallocated with delete is not necessarily the one that will cause a seg fault. Most likely, it will be another copy of the pointer that is unaware that the memory is freed and has no way of knowing. The common solution to this is reference counted "smart" pointers.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by IfYouSaySo
    The common solution to this is reference counted "smart" pointers.
    A good structure in your program would never put you in this position.
    Last edited by Magos; 02-22-2003 at 04:59 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    >A good structure in your program would bever put
    >you in this position.

    If you say so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. test void pointer before cast (instanceof)
    By jeanluca in forum C Programming
    Replies: 7
    Last Post: 05-29-2009, 04:26 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM