Thread: Deallocate Memory and reuse pointer

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    45

    Deallocate Memory and reuse pointer

    Hi,

    is it possible to deallocate memory, then use that same pointer after the delete statement

    e.g

    Code:
    delete ptr;
    ptr = 0;
    Or is that pointer unavailable to use when its memory it points to is unallocated

    Many Thanks
    -Alex

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You cannot dereference null pointers, but you can assign the same pointer object to different memory, and then it becomes safe to dereference, use pointer arithmetic, et cetera.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    so i can't delete the memory pointed to by say ptr and then allocate more memory using the same pointer (ptr)

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, you can do that. The point is that you have to make sure you allocate new memory before you use it again.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No, I just said you could do that. What you need to be careful of is
    * that the pointer you want to use is still in scope when you want to use it.
    * that you only direference or use pointer arithmatic while ptr contains an address.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    45
    Quote Originally Posted by Daved View Post
    No, you can do that. The point is that you have to make sure you allocate new memory before you use it again.
    ok, cool thought so!

    It did compile just didnt know if it was safe and correct

    -Alex

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    when in doubt, compile and run it. Worse case scenario* is you have to reboot.



    *that is unless you are doing hinky stuff that could dmg your system.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >when in doubt, compile and run it.
    That won't really tell you much.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    One guy posted code that didn't work in the C forums, when trying to compare two strings. When I pointed out his error, he wanted to know why another piece of code of his worked when he did it the same way. The program that he said worked produced the desired end results for him, although it was wrong since it was comparing memory addresses instead of C strings, although because of his compiler optomising the building of the string table, he didn't realize anything was wrong.

    Case in point: Just because something compiles, runs, and appears to give you the correct answer.... it doesn't mean it's actually right.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by abachler View Post
    when in doubt, compile and run it. Worse case scenario* is you have to reboot.
    No... The worst case scenario is that it seems to work just fine, but in fact is wrong and could blow up at any moment.

    A crashed system is a GREAT outcome because it tells you without a doubt that your code is wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free pointer?
    By jordanguyoflove in forum C Programming
    Replies: 9
    Last Post: 10-17-2008, 01:38 AM