Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #6926

Re: Should you free your pointers?

From Nobody <nobody@nowhere.com>
Subject Re: Should you free your pointers?
Date 2011-06-25 15:27 +0100
Message-Id <pan.2011.06.25.14.27.49.915000@nowhere.com>
Newsgroups comp.lang.c
References <iu2sov$3dv$1@speranza.aioe.org> <iu3ftj$9q1$1@dont-email.me>
Organization Zen Internet

Show all headers | View raw


On Fri, 24 Jun 2011 22:07:46 -0400, James Kuyper wrote:

>> In the case where you know that your program will be terminating after 
>> it is done using the pointers, should you even bother freeing the space up?

> It's not necessary, but I think it's easier to not make a special case
> of main(); the rules for creating well-written code are easier to
> remember if they don't have more special cases than they have to.

Sometimes, explicitly freeing memory requires added complexity in order
to keep track of what needs to be freed. E.g. if you have linked data
structures where you could have multiple pointers to a single block of
memory, you have to take steps to avoid multiple free()s of a single
block. Similarly if pointers can be to either static or dynamic data.

Adding such complexity is especially pointless if it only comes into play
upon termination, e.g. if you have complex structures which must exist in
order for the program to perform its normal function.

> Also, there's always the possibility that code from main() may someday
> get moved off to another function; if that code already contains
> properly connected malloc() and free() calls, that's one less thing to
> worry about during the move.

If the structure of the code is such that "pairing" malloc and free makes
sense, I'll do it. But in that case, I may set __free_hook (glibc) or
similar to point to an empty function once termination is assured. This
also deals with external libraries which typically have to free()
everything upon finalisation to be on the safe side.

Meticulously free()ing memory when you know that the program is about to
terminate is like rearranging the deckchairs as the Titanic is sinking.
Such operations can often take far longer than is immediately apparent, as
you often end up walking through memory which has long since been swapped
out.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Should you free your pointers? Billy Mays <noway@nohow.com> - 2011-06-24 16:41 -0400
  Re: Should you free your pointers? Shao Miller <sha0.miller@gmail.com> - 2011-06-24 16:51 -0400
    Re: Should you free your pointers? Shao Miller <sha0.miller@gmail.com> - 2011-06-24 16:56 -0400
    Re: Should you free your pointers? falk@rahul.net (Edward A. Falk) - 2011-06-26 04:09 +0000
  Re: Should you free your pointers? Nobody <nobody@nowhere.com> - 2011-06-24 21:53 +0100
    Re: Should you free your pointers? Martin Ambuhl <mambuhl@earthlink.net> - 2011-06-24 19:06 -0400
  Re: Should you free your pointers? Ben Pfaff <blp@cs.stanford.edu> - 2011-06-24 14:05 -0700
    Re: Should you free your pointers? David Mathog <dmathog@gmail.com> - 2011-06-28 09:36 -0700
  Re: Should you free your pointers? Ian Collins <ian-news@hotmail.com> - 2011-06-25 09:31 +1200
  Re: Should you free your pointers? Martin Ambuhl <mambuhl@earthlink.net> - 2011-06-24 19:04 -0400
  Re: Should you free your pointers? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-24 21:19 -0400
    Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-25 15:15 -0700
      Re: Should you free your pointers? Keith Thompson <kst-u@mib.org> - 2011-06-25 15:44 -0700
        Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-26 12:56 -0700
          Re: Should you free your pointers? Keith Thompson <kst-u@mib.org> - 2011-06-26 13:08 -0700
            Re: Should you free your pointers? Geoff <geoff@invalid.invalid> - 2011-06-26 14:33 -0700
            Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-26 16:27 -0700
        Re: Should you free your pointers? James Kuyper <jameskuyper@verizon.net> - 2011-06-26 19:39 -0400
          Re: Should you free your pointers? Keith Thompson <kst-u@mib.org> - 2011-06-26 19:28 -0700
            Re: Should you free your pointers? James Waldby <not@valid.invalid> - 2011-06-27 05:42 +0000
            Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-27 12:59 -0700
              Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-27 13:06 -0700
              Re: Should you free your pointers? Seebs <usenet-nospam@seebs.net> - 2011-06-27 20:26 +0000
              Re: Should you free your pointers? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-27 14:28 -0600
              Re: Should you free your pointers? pacman@kosh.dhis.org (Alan Curry) - 2011-06-27 23:29 +0000
              Re: Should you free your pointers? pete <pfiland@mindspring.com> - 2011-06-27 21:08 -0400
              Re: Should you free your pointers? "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-28 04:05 +0100
                Re: Should you free your pointers? Billy Mays <noway@nohow.com> - 2011-06-28 00:25 -0400
                Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-28 01:23 -0700
                Re: Should you free your pointers? Shao Miller <sha0.miller@gmail.com> - 2011-06-28 01:00 -0500
                Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-28 01:22 -0700
                Re: Should you free your pointers? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-28 10:02 -0600
                Re: Should you free your pointers? "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-29 02:41 +0100
                Re: Should you free your pointers? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-06-28 19:47 -0600
              Re: Should you free your pointers? James Kuyper <jameskuyper@verizon.net> - 2011-06-28 08:21 -0400
                Re: Should you free your pointers? Michael Press <rubrum@pacbell.net> - 2011-06-30 15:43 -0700
              Re: Should you free your pointers? Keith Thompson <kst-u@mib.org> - 2011-06-28 22:36 -0700
  Re: Should you free your pointers? James Kuyper <jameskuyper@verizon.net> - 2011-06-24 22:07 -0400
    Re: Should you free your pointers? Nobody <nobody@nowhere.com> - 2011-06-25 15:27 +0100
  Re: Should you free your pointers? China Blue Dolls <chine.bleu@yahoo.com> - 2011-06-27 18:16 -0700
  Re: Should you free your pointers? DDD <1983ddd@gmail.com> - 2011-06-28 19:41 -0700

csiph-web