question about free()

This is a discussion on question about free() within the C Programming forums, part of the General Programming Boards category; Can I free a pointer several times? Or, can I free a pointer if I don't know whether it has ...

  1. #1
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27

    question about free()

    Can I free a pointer several times?
    Or, can I free a pointer if I don't know whether it has been freed?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    No. You must only free a pointer once, and only free pointers returned by a call to malloc(). (Or if it's some other memory allocation funtion: read the reference!)

    For the C function free(), if you pass a pointer already free()'d or something not returned by malloc(), calloc(), or realloc(), you risk undefined behavior.
    See malloc().

    It is up to your program to keep track of what needs to be free()'d. An operating system should free any memory still allocated upon program termination. However, this doesn't mean you should just forget about freeing. Programs can eat memory quickly in a loop or after long runs. Personally, I try to free everything I allocate myself.
    Last edited by Cactus_Hugger; 05-18-2006 at 11:10 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I concur. I always free everything I allocate myself. It is my opinion that if I ever am in the position where I cannot free something I've allocated, it's time to redesign, because I've really messed something up. If it's too hard to free what you've allocated, you're doing something wrong, IMO.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Run! Forrest Gump! TalosChen's Avatar
    Join Date
    May 2006
    Location
    Shanghai
    Posts
    27
    Thanks. I've always learned much here.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can set a pointer to NULL to indicate it has been freed.
    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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But setting it to NULL is not the same as freeing, at least not in C. dwks wasn't implying that, but for your clarification, I thought I'd point it out.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design question
    By MacGyver in forum C Programming
    Replies: 9
    Last Post: 05-17-2007, 02:36 AM
  2. Simple question about free()
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-16-2006, 11:14 AM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 01:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 07:52 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21