Thread: Deleting Pointers?

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by dwks View Post
    Correct.

    Just declaring a pointer, and setting it to point at memory allocated for another variable, does not use any memory. In your case, you don't have to free or delete currentNode at all.

    Think of it this way. Every new should have one matching delete. Thus, currentNode does not need to be deleted because it was never new'ed.
    Ahhhh ok. I get it! Good way of putting it.

    Ok, now what happens if I don't do anything with that pointer.

    And then back in main, I call delete. Are those pointers within the function still pointing to the object? So it will cause a seg fault?

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Remember: pointers are the addresses.
    Duplicating the address will do nothing to the house. As soon as you call delete on one pointer, it will demolish the house it points to, so all pointers will now contain an address to a demolished house. The pointers are still valid, but the address is not.

    If you demolish the house first and then try to use it is a way, you will (likely) get a seg fault.
    Similarly, just defining (creating) pointers will not create a house. The pointers will contain a "random" address, which may or may not, contain the address of a real house (therefore you may call it undefined).
    It is the operator new (keyword) that actually creates a house. So long as the house exists, you can copy the address indefinetly and use it indefinetely, as long as you want, until you call delete, upon which time the house is demolished and the address is invalid.
    Last edited by Elysia; 07-24-2008 at 12:47 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> And then back in main, I call delete. Are those pointers within the function still pointing to the object?
    If you're back in main, then the function is done. The execution flow of a program follows a specific path. If you call a function and it uses a pointer that points to an object that has been deleted, you will have problems.

    There is a concept called ownership of a resource. When you use new to allocate memory, you are creating a resource. Something has to own that resource. Whatever owns that resource is responsible for cleaning it up (in this case by deleting it). Your task as a C++ programmer is to identify which part of the code owns the resource, and make sure that no other code will attempt to use it after the owner has destroyed it.

    Of course, in C++, there are many tools available that make managing resources easier. For example, the vector class helps manage a dynamic array. Smart pointers like shared_ptr or auto_ptr help manage raw pointers like the ones you're using. There are container classes like set that do the dirty work and provide most of what you need in a data structure. You should strive to use those tools to make a better problem that doesn't have as many errors when it comes to managing resources. (That doesn't mean it's not a good idea to learn how to do it yourself, though, just don't do it yourself in your real programs if you don't have to.)

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Alrighty, I've learned quite a lot about pointers today! Thanks guys, awesome analogies hehe So I changed a bunch of stuff around in my code, and it worked for everything except for the last pointers when I'm about to quit the program.

    I have 5 pointers I need to delete. Two of them delete fine ("t" and "nr").

    Three of them cause seg faults ("circ", "ptr2set" and "ptr2elem").

    "circ" and "ptr2set" have "Bad Ptr" values when I debug

    Screenshot of Visual Studio Debugging: http://img526.imageshack.us/img526/9804/badptrqt2.png

    A) Is "Bad Ptr" a double deletion error?

    -----

    And the 5th pointer, "ptr2elem", causes a seg fault as well, but doesn't have the value "Bad Ptr" when debugging. Instead, this happens: http://img296.imageshack.us/img296/3...tr2elemvx1.png

    B) What the heck does that mean?
    Last edited by Paul22000; 07-24-2008 at 08:35 PM.

  5. #20
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unfortunately, this is a really bad way of solving this issue. You can't rely on the debugger to tell you what to delete and what not to. You have to design and understand the logic of your code to figure out what to delete. Every pointer address returned by new should be deleted once and only once. If you make copies of that address then you need to keep track of the copies and make sure only one is the official copy that gets deleted.

    If you have trouble doing this, then you should consider using the tools C++ gives you that manage this for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-28-2008, 02:46 AM
  2. deleting pointers in the destructor
    By jsrig88 in forum C++ Programming
    Replies: 10
    Last Post: 01-03-2008, 02:01 AM
  3. Array of Pointers + Deleting An Object = Problems
    By Nereus in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2004, 12:16 PM
  4. Help with deleting array of pointers
    By registering in forum C++ Programming
    Replies: 4
    Last Post: 10-09-2003, 11:47 AM
  5. deleting pointers....
    By DirX in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2003, 02:49 PM