Do memory allocations need to be freeed every time?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ravindra.B

    Do memory allocations need to be freeed every time?

    I have declared a global variable which is array of pointers and
    allocated memory for each array variable by using malloc. Some thing
    similar to below...

    static char *arr[10];

    main()
    {
    int i;

    for(i = 0; i < 10; i++)
    arr[i] = malloc(sizeof(c har));

    ……..

    }

    Is freeing the allocated memory is really required every time, for a
    program like this as freeing doesn’t bring in any change w.r.t
    memory space (or) will it become a memleak some where down the line if
    it is not freeed?

    Thanx,
    Ravindra. Bhadramraju
  • Richard Heathfield

    #2
    Re: Do memory allocations need to be freeed every time?

    Ravindra.B said:

    <snip>
    Is freeing the allocated memory is really required every time, for a
    program like this as freeing doesn?t bring in any change w.r.t
    memory space (or) will it become a memleak some where down the line if
    it is not freeed?
    Best practice - take only what you need, make sure you've got it, and give
    it back when you're done with it. If you ignore best practice, one day
    you'll find yourself with a huge program, full of potential leaks, which
    suddenly needs to become Just Another Function that is called many times
    in a loop by the new main(), at which point the potential leaks become
    actual leaks, and cause you months of work.

    If you do it right, right from the start, you'll be fine in days to come -
    except that you'll no doubt end up working on a project where some clown
    has got it wrong because he ignored my advice, and you'll have to fix his
    stuff, but at least you'll have the moral authority to curse the ground he
    walks on.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • vippstar@gmail.com

      #3
      Re: Do memory allocations need to be freeed every time?

      On Jun 19, 12:23 pm, "Ravindra.B " <ravindra.bhadr amr...@gmail.co m>
      wrote:
      I have declared a global variable which is array of pointers and
      allocated memory for each array variable by using malloc. Some thing
      similar to below...
      >
      <snip code>
      >
      Is freeing the allocated memory is really required every time, for a
      Yes. *Every* malloc pointer must be freed.

      Comment

      • den2k

        #4
        Re: Do memory allocations need to be freeed every time?

        On 19 Giu, 11:23, "Ravindra.B " <ravindra.bhadr amr...@gmail.co mwrote:
        Is freeing the allocated memory is really required every time, for a
        program like this as freeing doesn’t bring in any change w.r.t
        memory space (or) will it become a memleak some where down the line if
        it is not freeed?
        >
        Thanx,
        Ravindra. Bhadramraju

        Let's say that it is a good practice. It is reasonable to think that
        this piece of code will be used somewhere in a bigger application,
        which may be executed continuosly for a lot of time whitout being
        closed, and this may lead to mem leak.

        Besides not every OS frees memory after the execution of a program and
        many OSs may not free it due to an abnormal termination, so it's
        always better to explicitly free memory by yourself.

        Comment

        • Chris Dollin

          #5
          Re: Do memory allocations need to be freeed every time?

          vippstar@gmail. com wrote:
          On Jun 19, 12:23 pm, "Ravindra.B " <ravindra.bhadr amr...@gmail.co m>
          wrote:
          >I have declared a global variable which is array of pointers and
          >allocated memory for each array variable by using malloc. Some thing
          >similar to below...
          >>
          <snip code>
          >>
          >Is freeing the allocated memory is really required every time, for a
          Yes. *Every* malloc pointer must be freed.
          /Should/, not /must/.

          (and " ... eventually".)

          --
          "I am afraid that this theory is quite untenable." /The Caves of Steel/

          Hewlett-Packard Limited registered no:
          registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

          Comment

          • jacob navia

            #6
            Re: Do memory allocations need to be freeed every time?

            Ravindra.B wrote:
            I have declared a global variable which is array of pointers and
            allocated memory for each array variable by using malloc. Some thing
            similar to below...
            >
            static char *arr[10];
            >
            main()
            {
            int i;
            >
            for(i = 0; i < 10; i++)
            arr[i] = malloc(sizeof(c har));
            >
            ……..
            >
            }
            >
            Is freeing the allocated memory is really required every time, for a
            program like this as freeing doesn’t bring in any change w.r.t
            memory space (or) will it become a memleak some where down the line if
            it is not freeed?
            >
            Thanx,
            Ravindra. Bhadramraju
            You can avoid the error prone usage of free by using a garbage
            collector. This software will free automatically the allocated
            memory for you when it notices that is no longer used.

            Good compilers provide this facility in their standard distribution.

            --
            jacob navia
            jacob at jacob point remcomp point fr
            logiciels/informatique

            Comment

            • Ravindra.B

              #7
              Re: Do memory allocations need to be freeed every time?

              On 19 Jun, 14:45, jacob navia <ja...@nospam.c omwrote:
              Ravindra.B wrote:
              I have declared a global variable which is array of pointers and
              allocated memory for each array variable by using malloc.  Some thing
              similar to below...
              >
              static char *arr[10];
              >
              main()
              {
              int i;
              >
              for(i = 0; i < 10; i++)
              arr[i] = malloc(sizeof(c har));
              >
              ……..
              >
              }
              >
              Is freeing the allocated memory is really required every time, for a
              program like this as freeing doesn’t  bring in  any change w.r.t
              memory space (or) will it become a memleak some where down the line if
              it is not freeed?
              >
              Thanx,
              Ravindra. Bhadramraju
              >
              You can avoid the error prone usage of free by using a garbage
              collector. This software will free automatically the allocated
              memory for you when it notices that is no longer used.
              >
              Good compilers provide this facility in their standard distribution.
              >
              --
              jacob navia
              jacob at jacob point remcomp point fr
              logiciels/informatiquehtt p://www.cs.virginia .edu/~lcc-win32- Hide quoted text -
              >
              - Show quoted text -
              That sounds interesting.

              Can you tell me what are all the compilers that support it and how to
              configure the same.

              Thanx,
              Ravindra.Bhadra mraju

              Comment

              • den2k

                #8
                Re: Do memory allocations need to be freeed every time?

                On 19 Giu, 11:45, jacob navia <ja...@nospam.c omwrote:
                >
                Good compilers provide this facility in their standard distribution.
                >
                --
                jacob navia
                jacob at jacob point remcomp point fr
                logiciels/informatiquehtt p://www.cs.virginia .edu/~lcc-win32

                IMHO the better compiler stays in the middle of our ears, so there
                must be the GC.. it is better to free manually, so you do know exactly
                how your code behaves.

                Comment

                • vippstar@gmail.com

                  #9
                  Re: Do memory allocations need to be freeed every time?

                  On Jun 19, 12:59 pm, "Ravindra.B " <ravindra.bhadr amr...@gmail.co m>
                  wrote:
                  On 19 Jun, 14:45, jacob navia <ja...@nospam.c omwrote:
                  >
                  >
                  >
                  Ravindra.B wrote:
                  I have declared a global variable which is array of pointers and
                  allocated memory for each array variable by using malloc. Some thing
                  similar to below...
                  >
                  static char *arr[10];
                  >
                  main()
                  {
                  int i;
                  >
                  for(i = 0; i < 10; i++)
                  arr[i] = malloc(sizeof(c har));
                  >
                  ……..
                  >
                  }
                  >
                  Is freeing the allocated memory is really required every time, for a
                  program like this as freeing doesn’t bring in any change w.r.t
                  memory space (or) will it become a memleak some where down the line if
                  it is not freeed?
                  >
                  Thanx,
                  Ravindra. Bhadramraju
                  >
                  You can avoid the error prone usage of free by using a garbage
                  collector. This software will free automatically the allocated
                  memory for you when it notices that is no longer used.
                  >
                  Good compilers provide this facility in their standard distribution.
                  >
                  --
                  jacob navia
                  jacob at jacob point remcomp point fr
                  logiciels/informatiquehtt p://www.cs.virginia .edu/~lcc-win32-Hide quotedtext -
                  >
                  - Show quoted text -
                  >
                  That sounds interesting.
                  >
                  Can you tell me what are all the compilers that support it and how to
                  configure the same.
                  Do not quote sig blocks.
                  No, it's not interesting. These garbage collectors are error prone,
                  free() is not.
                  I've never heard of a good C compiler providing a GC.
                  They are also off-topic to discuss here. I, and others have, already
                  answered you:
                  You *must* free() allocated memory.
                  (Or, with Mr Dollins correction: you should, eventually)

                  Comment

                  • viza

                    #10
                    Re: Do memory allocations need to be freeed every time?

                    Hi

                    On Thu, 19 Jun 2008 02:23:05 -0700, Ravindra.B wrote:
                    Is freeing the allocated memory is really required every time, for a
                    program like this as freeing doesn’t bring in any change w.r.t memory
                    space (or) will it become a memleak some where down the line if it is
                    not freeed?
                    To avoid memory leaks, for each and every time you call malloc(), you
                    must call free() exactly once with the same pointer that was returned.

                    The only exceptions to this are:

                    1) if the call to malloc() returned NULL
                    2) if you are about to immediately terminate the program anyway*

                    * see

                    be39423f62c88e0 4/a8e1101586394d4 2

                    HTH
                    viza

                    Comment

                    • Richard Bos

                      #11
                      Re: Do memory allocations need to be freeed every time?

                      viza <tom.viza@gmil. comwrote:
                      On Thu, 19 Jun 2008 02:23:05 -0700, Ravindra.B wrote:
                      >
                      Is freeing the allocated memory is really required every time, for a
                      program like this as freeing doesn’t bring in any change w.r.t memory
                      space (or) will it become a memleak some where down the line if it is
                      not freeed?
                      >
                      To avoid memory leaks, for each and every time you call malloc(), you
                      must call free() exactly once with the same pointer that was returned.
                      >
                      The only exceptions to this are:
                      >
                      1) if the call to malloc() returned NULL
                      2) if you are about to immediately terminate the program anyway*
                      And even in those cases, you _may_ call free(), and it is often
                      considered good style (and particularly in the first case, can simplify
                      your code) if you do.

                      Richard

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Do memory allocations need to be freeed every time?

                        Ravindra.B said:
                        On 19 Jun, 14:45, jacob navia <ja...@nospam.c omwrote:
                        <snip>
                        >You can avoid the error prone usage of free by using a garbage
                        >collector. This software will free automatically the allocated
                        >memory for you when it notices that is no longer used.
                        >>
                        >Good compilers provide this facility in their standard distribution.
                        >
                        That sounds interesting.
                        Perhaps. Partitioned Data Sets are quite interesting, too, in their own
                        quiet way, but that doesn't make them relevant in comp.lang.c.

                        Note that "standard distribution" *doesn't* mean "garbage collection is
                        standard". It isn't standard. In the context of this newsgroup, "standard"
                        can reasonably be interpreted as meaning "available in all
                        implementations , or at least in all hosted implementations "; when someone
                        uses the term in some other way without pointing out the fact, it is
                        reasonable to assume that they are either lying, mistaken, or simply
                        clueless.
                        Can you tell me what are all the compilers that support it and how to
                        configure the same.
                        No compiler is *required* to provide garbage collection. The C language
                        doesn't provide this facility. If you come to rely on it, a time may well
                        come when you (think you) need it but can't have it. At that point, you
                        will realise why others here have been encouraging you to learn how to
                        manage memory yourself.

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -http://www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Richard Heathfield

                          #13
                          Re: Do memory allocations need to be freeed every time?

                          viza said:
                          Hi
                          >
                          On Thu, 19 Jun 2008 02:23:05 -0700, Ravindra.B wrote:
                          >
                          >Is freeing the allocated memory is really required every time, for a
                          >program like this as freeing doesn?t bring in any change w.r.t memory
                          >space (or) will it become a memleak some where down the line if it is
                          >not freeed?
                          >
                          To avoid memory leaks, for each and every time you call malloc(), you
                          must call free() exactly once with the same pointer that was returned.
                          >
                          The only exceptions to this are:
                          >
                          1) if the call to malloc() returned NULL
                          2) if you are about to immediately terminate the program anyway*
                          The calloc and realloc functions muddy the waters a little. The calloc
                          function can simply be thought of as malloc with attitude, but realloc
                          needs a little care.

                          char *p = malloc(m * sizeof *p);
                          if(p != NULL)
                          {
                          char *q = realloc(p, n * sizeof *p);

                          In this case, when we eventually call free(), it is q that we must pass.
                          Passing p to free() would be an error, even though we got it from malloc
                          and haven't previously passed it to free().

                          --
                          Richard Heathfield <http://www.cpax.org.uk >
                          Email: -http://www. +rjh@
                          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                          "Usenet is a strange place" - dmr 29 July 1999

                          Comment

                          • jacob navia

                            #14
                            Re: Do memory allocations need to be freeed every time?

                            Ravindra.B wrote:
                            >
                            That sounds interesting.
                            >
                            Can you tell me what are all the compilers that support it and how to
                            configure the same.
                            >
                            The most popular garbage collector is Boehm's GC. It works with gcc
                            (you need to install it separatedly).

                            lcc-win (http://www.cs.virginia.edu/~lcc-win32) features a GC in
                            the standard distribution.

                            Note that if you download lcc-win, you can use gc.dll (32 bits
                            only) with another compiler if you wish. That would make it
                            possible to use a GC with ANY windows compiler.

                            Boehm's GC runs in almost all serious operating systems, wchich
                            includes several varints of HP unix, linux, MAC os, etc.

                            lcc-win has ported the GC to 64 bit windows and will be
                            distributed with the 64 bit version of lcc-win.


                            --
                            jacob navia
                            jacob at jacob point remcomp point fr
                            logiciels/informatique

                            Comment

                            • santosh

                              #15
                              Re: Do memory allocations need to be freeed every time?

                              Ravindra.B wrote:
                              On 19 Jun, 14:45, jacob navia <ja...@nospam.c omwrote:
                              >Ravindra.B wrote:
                              [ ... ]
                              Is freeing the allocated memory is really required every time, for
                              a program like this as freeing doesn?t  bring in  any change w.r.t
                              memory space (or) will it become a memleak some where down the line
                              if it is not freeed?
                              >>
                              >You can avoid the error prone usage of free by using a garbage
                              >collector. This software will free automatically the allocated
                              >memory for you when it notices that is no longer used.
                              >>
                              >Good compilers provide this facility in their standard distribution.
                              >>
                              >--
                              >jacob navia
                              >jacob at jacob point remcomp point fr
                              >logiciels/informatiquehtt p://www.cs.virginia .edu/~lcc-win32- Hide
                              >quoted text -
                              >>
                              >- Show quoted text -
                              Don't retain signatures unless you are discussing them. Signature blocks
                              are text following a "\n-- \n" sequence.
                              That sounds interesting.
                              >
                              Can you tell me what are all the compilers that support it and how to
                              configure the same.
                              Be aware that Garbage Collection is *not* a part of ISO C. If you want
                              your code to be portable to as many implementations as feasible, then a
                              Garbage Collector is likely to be a hindrance than help. Also Garbage
                              Collectors are not quite deterministic - they may very rarely free
                              memory in use or more commonly, fail to free memory that ought to be
                              freed. The might also not be suitable for program that demand realtime
                              response to events.

                              Having said all this, if you still want to tie your code to a GC, then
                              one of the better ones is the Boehm GC.

                              <http://www.hpl.hp.com/personal/Hans_Boehm/gc/>

                              Comment

                              Working...