Thread: Pointer memory question

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    8

    Pointer memory question

    So I started the linked list tutorial here and decided to put my understanding to a test. The objective: write code that could go through a list, delete all the pointers, and reclaim the memory.

    I did it (I hope), but that spawned another question -- is deleting pointers in that way worthwhile? Are there safeguards in place to prevent memory leaks or whatever consequences would arise if the pointers weren't individually deleted during the program's runtime?

    Thanks a lot

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Edo
    Are there safeguards in place to prevent memory leaks or whatever consequences would arise if the pointers weren't individually deleted during the program's runtime?
    A modern operating would likely clean up after the program. Note that you are not so much deleting pointers as destroying the dynamically allocated objects via the pointers.

    Quote Originally Posted by Edo
    is deleting pointers in that way worthwhile?
    Yes. For one thing, memory leaks are still undesirable during the run of the program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Memory leaks are exactly the problem caused by not deleting (or freeing) memory that has been allocated elsewhere.

    In a small, simple application that runs for a few seconds, it's probably no big deal to forget to delete the memory when you are done [1] with it. In a large, long-running, application, it can have a consequence of the application crashing or otherwise misbehaving due to inability to allocate more memory - after all, no matter how large the memory is, it HAS some sort of limit, and you WILL run out at some point if you keep allocating more and more memory for a long enough time. If that is 10 minutes, 10 days or 10 months is of course a different matter.

    It is of course also a case of completeness and correctness, and it's much easier to have a good style of always deleting everything allocated with new by deleting it again when finished.

    [1]If we have more complex OBJECTS that are created with new, then the destruction called by delete will perhaps do other things than just freeing memory, in which case there may be other side-effects of not deleting the object is that it also leaves other resources (file objects or other system resources that may be a alot more limited than memory), which can lead to badness much earlier than "out of memory after 10 days".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    8
    Thanks for your responses guys, I'll stay aware of the allocations my code makes.

    Note that you are not so much deleting pointers as destroying the dynamically allocated objects via the pointers.
    So, as an example, I should think of
    Code:
    delete point_to_struct; point_to_struct = 0;
    as a way to destroy and recoup the memory from that instance of the structure...?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes.

    Code:
    point_to_struct = 0;
    This isn't strictly necessary. Otherwise the pointer will simply keep pointing to the same memory location where the struct instance used to be. If you intend to keep the pointer around for longer, setting it to 0 allows you to tell apart valid and invalid pointers.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointers should typically be set to NULL, however (or nullptr in C++0x).
    You can also make a "safe" delete function:
    Code:
    template<typename T> void safe_delete(T& p)
    {
        delete p;
        p = NULL;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Pointers" <-- crappy name
    By argv in forum General Discussions
    Replies: 64
    Last Post: 06-25-2009, 08:18 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Replies: 8
    Last Post: 01-04-2006, 07:42 PM
  4. Question regarding constructors and memory.....
    By INFERNO2K in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2005, 11:30 AM
  5. memory question (pointer related)
    By cjschw in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2004, 01:09 PM