Is realloc good form ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guillaume Dargaud

    Is realloc good form ?

    Hello all,
    I have a 'good practice' question.
    Lately I've been using a lot of functions such as this:

    void Func(int Size, double *Array) {
    static double *Transformed=NU LL;
    Transformed=rea lloc(Transforme d, Size*sizeof(dou ble));
    // Do something from Array to Transformed
    }

    Now if the value of Size changes a lot between calls, the resulting prog is
    poorly optimized (it reallocates each time). I'm ok with that.
    Ignoring the fact that the memory is never freed, if the value of Size
    changes seldom, does the call to realloc wastes time then ?
    --
    Guillaume Dargaud



  • christian.bau

    #2
    Re: Is realloc good form ?

    On Oct 18, 10:02 am, "Guillaume Dargaud"
    <use_the_form_o n_my_contact_p. ..@www.gdargaud .netwrote:
    Hello all,
    I have a 'good practice' question.
    Lately I've been using a lot of functions such as this:
    >
    void Func(int Size, double *Array) {
    static double *Transformed=NU LL;
    Transformed=rea lloc(Transforme d, Size*sizeof(dou ble));
    // Do something from Array to Transformed
    >
    }
    >
    Now if the value of Size changes a lot between calls, the resulting prog is
    poorly optimized (it reallocates each time). I'm ok with that.
    Ignoring the fact that the memory is never freed, if the value of Size
    changes seldom, does the call to realloc wastes time then ?
    It depends. realloc will often be clever enough to realise that it can
    just use the previous block of data without any change. So if you pass
    in the same Size or similar Size values (like 10000, 10001, 9999) it
    will be fast. However, if a new block is allocated, all the data will
    be copied from the previous block to the new block, because that is
    what realloc does. That might be quite wasteful.

    You might just add another variable "static int allocatedSize = 0",
    compare new and old size and only realloc when it gets bigger.

    Comment

    • vipvipvipvip.ru@gmail.com

      #3
      Re: Is realloc good form ?

      On Oct 18, 1:28 pm, "christian. bau" <christian....@ cbau.wanadoo.co .uk>
      wrote:
      It depends. realloc will often be clever enough to realise that it can
      just use the previous block of data without any change. So if you pass
      in the same Size or similar Size values (like 10000, 10001, 9999) it
      will be fast. However, if a new block is allocated, all the data will
      be copied from the previous block to the new block, because that is
      what realloc does. That might be quite wasteful.
      realloc tries not to copy the data, just resize the available block of
      memory.
      Note that realloc() *may* move the memory allocation resulting in a different return value than ptr.

      Comment

      • Spiros Bousbouras

        #4
        Re: Is realloc good form ?

        On Oct 18, 11:28 am, "christian. bau"
        <christian....@ cbau.wanadoo.co .ukwrote:
        On Oct 18, 10:02 am, "Guillaume Dargaud"
        >
        <use_the_form_o n_my_contact_p. ..@www.gdargaud .netwrote:
        Hello all,
        I have a 'good practice' question.
        Lately I've been using a lot of functions such as this:
        >
        void Func(int Size, double *Array) {
        static double *Transformed=NU LL;
        Transformed=rea lloc(Transforme d, Size*sizeof(dou ble));
        // Do something from Array to Transformed
        >
        }
        >
        Now if the value of Size changes a lot between calls, the resulting prog is
        poorly optimized (it reallocates each time). I'm ok with that.
        Ignoring the fact that the memory is never freed, if the value of Size
        changes seldom, does the call to realloc wastes time then ?
        If you're ok with the fact that the function calls realloc each time,
        then which waste of time are you worried about ?

        Leaving your question aside there are a couple of problems with the
        function. It would be better to make Size of type size_t. You need to
        make sure that the expression Size*sizeof(dou ble) does not overflow
        before you pass it to realloc.
        You might just add another variable "static int allocatedSize = 0",
        compare new and old size and only realloc when it gets bigger.
        I second that apart from the fact that allocatedSize also needs to be
        size_t.

        Comment

        • Mark Bluemel

          #5
          Re: Is realloc good form ?

          vipvipvipvip.ru @gmail.com wrote:
          realloc tries not to copy the data, just resize the available block of
          memory.
          Does the standard enforce this? Otherwise you are simply discussing what
          some (possibly all, but I doubt you could prove that) implementations do.

          Comment

          • Richard Heathfield

            #6
            Re: Is realloc good form ?

            Mark Bluemel said:
            vipvipvipvip.ru @gmail.com wrote:
            >
            >realloc tries not to copy the data, just resize the available block of
            >memory.
            >
            Does the standard enforce this?
            No.
            Otherwise you are simply discussing what
            some (possibly all, but I doubt you could prove that) implementations do.
            Right.

            --
            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

            • CBFalconer

              #7
              Re: Is realloc good form ?

              vipvipvipvip.ru @gmail.com wrote:
              "christian. bau" <christian....@ cbau.wanadoo.co .ukwrote:
              >
              >It depends. realloc will often be clever enough to realise that
              >it can just use the previous block of data without any change.
              >So if you pass in the same Size or similar Size values (like
              >10000, 10001, 9999) it will be fast. However, if a new block is
              >allocated, all the data will be copied from the previous block
              >to the new block, because that is what realloc does. That might
              >be quite wasteful.
              >
              realloc tries not to copy the data, just resize the available
              block of memory.
              That depends on the malloc/free/realloc package, and is not
              guaranteed. One package that goes to lengths to avoid copying is
              nmalloc for DJGPP and similar, available at:

              <http://cbfalconer.home .att.net/download/>

              --
              Chuck F (cbfalconer at maineline dot net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net>



              --
              Posted via a free Usenet account from http://www.teranews.com

              Comment

              • christian.bau

                #8
                Re: Is realloc good form ?

                On Oct 19, 8:32 pm, Keith Thompson <ks...@mib.orgw rote:
                Also, even if you try to reallocate the same size, I can imagine
                realloc() noticing that the chunk is in the middle of a region of
                mostly free space, and that it can help future allocations by
                relocating it so it's adjacent to other allocated space. I don't know
                whether any actual allocators do this.
                I have used implementations used for debugging that would _always_
                allocate a new block on purpose. That makes bugs where someone didn't
                expect a changed pointer more obvious.


                Comment

                • Kenneth Brody

                  #9
                  Re: Is realloc good form ?

                  "christian. bau" wrote:
                  >
                  On Oct 19, 8:32 pm, Keith Thompson <ks...@mib.orgw rote:
                  >
                  Also, even if you try to reallocate the same size, I can imagine
                  realloc() noticing that the chunk is in the middle of a region of
                  mostly free space, and that it can help future allocations by
                  relocating it so it's adjacent to other allocated space. I don't know
                  whether any actual allocators do this.
                  >
                  I have used implementations used for debugging that would _always_
                  allocate a new block on purpose. That makes bugs where someone didn't
                  expect a changed pointer more obvious.
                  Sounds reasonable, especially if the old block's contents are trashed
                  at the same time.

                  --
                  +-------------------------+--------------------+-----------------------+
                  | Kenneth J. Brody | www.hvcomputer.com | #include |
                  | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
                  +-------------------------+--------------------+-----------------------+
                  Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                  Comment

                  • CBFalconer

                    #10
                    Re: Is realloc good form ?

                    Kenneth Brody wrote:
                    >
                    .... snip ...
                    >
                    I have seen implementations take a shrinking realloc(), and merge
                    the released space with an adjoining free chunk, creating a single,
                    larger chunk. (I would suspect that this is relatively common.)
                    Here is an extraction from nmalloc.c, with most code removed,
                    showing the cases handled to avoid any unnecessary memory copying.

                    /* if decreasing simply reduce size and move excess to free */
                    else if (szneed ((ulong)(INT_MA X - 65536))) {
                    /* reject excessive size request */
                    p = NULL; goto exeunt;
                    }
                    else if (ISFREE(m->next) &&
                    (szneed <= (m->sz + m->next->sz)) ) {
                    /* the 'next' block is free and adequate so use it */
                    /* else m is the oversized return block */
                    }
                    else if ((lastsbrk == m->next) &&
                    ((szneed + MINSAVE) <= (m->sz + lastsbrk->sz)) ) {
                    /* lastsbrk is adequate and adjacent so use it */
                    }
                    else if (ISFREE(m->prev) &&
                    (szneed <= (m->sz + m->prev->sz)) ) {
                    /* the 'prev' block is free and adequate so use it */
                    }
                    else if ((b = searchfree(szne ed))) {
                    /* An adequate free block exists, copy over, free old */
                    }
                    else if (lastsbrk &&
                    ((szneed + MINSAVE) <= lastsbrk->sz) ) {
                    DBGPRTR(EOL " Realloc is copying into lastsbrk");
                    }
                    /* else malloc new size, copy data, and free old */
                    else if ((m1 = extendsbrk(szne ed))) {
                    if (lastsbrk == m->next) {
                    DBGPRTR(EOL " Realloc is now using lastsbrk extended");
                    }
                    else {
                    /* At this point lastsbrk is adequate size */
                    /* split off, copy over, and free old */
                    }
                    }
                    else m = NULL; /* failure */

                    You can find the complete code on my site (URL in sig).

                    --
                    Chuck F (cbfalconer at maineline dot net)
                    Available for consulting/temporary embedded and systems.
                    <http://cbfalconer.home .att.net>


                    --
                    Posted via a free Usenet account from http://www.teranews.com

                    Comment

                    Working...