Thread: how to check list is free

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    20

    how to check list is free

    For the storage management, we can use free() function, but the storage management functions might have bugs, like accidentally freeing a section of memory twice, or overwriting an array. Some of these bugs will result in infinite loops or crashes. How to define a function to check the list is freed? like detect is a missing free block that is a block that's been freed that doesn't appear in the free list. Can anyone give some idea for that? Thanks!!!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    free(ptr);
    ptr = NULL;
    Since free(NULL) has no effect it's fine if you free it again.

    Unless I've missed what you're asking?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use some external program that will make such checks for you like Boundschecker

    or you can write your own memory manager - that will allocate a big memory pool at the beginning of the program and then allocate and deallocate memory ranges from this pool - adding necessary checks
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    20
    Quote Originally Posted by zacs7 View Post
    Code:
    free(ptr);
    ptr = NULL;
    Since free(NULL) has no effect it's fine if you free it again.

    Unless I've missed what you're asking?

    just like you said after freed ptr then assign prt = NULL, it work fine. do we have any other method that checks the consistency of the free list?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Keep track, programmatically, what has been freed and what has not. For example, only free the memory for something in one function.

    How to define a function to check the list is freed? like detect is a missing free block that is a block that's been freed that doesn't appear in the free list. Can anyone give some idea for that?
    Given a pointer, there's no way to tell if it's pointing to allocated memory or not -- unless you set the pointer to NULL when it is not allocated (i.e., before malloc() and after free()). Then you can just compare the pointer against NULL.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Two ways to build software:
    (1) make it so simple that there are obviously no bugs,
    (2) make it so complicated that there are no obvious bugs.
    -- C.A.R. Hoare (1980 Turing Award Lecture)

    Feel free to replace the word 'bug' above with the words 'memory leaks'.
    My advice is, stop going down the path of number 2.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM