Re: realloc but not copy




<robertwessel2@xxxxxxxxx> wrote in message
news:1161653531.600769.322170@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Eric Sosman wrote:
ivan.leben@xxxxxxxxx wrote:

Let's say I have a piece of allocated memory which I want to expand and
reuse if possible or allocate in a different part of RAM if resizing is
not possible, however, in the latter case I don't care about the old
data and I don't need to copy it to the new location. Is there a
standard function that could do that? As far as I understand the
description of the realloc() function, it _always_ copies the data to
the new location, even if that is not desired. Can I somehow just
_check_ if the expansion is possible and invoke a new malloc() myself
in case it is not? I'd like this operation to be as fast as possible
because the size of the memory I need is very large.

free(ptr);
ptr = malloc(newsize);
if (ptr == NULL) ...

Or to look at it another way: Why do you prefer to re-use
the memory already allocated, as opposed to some other piece of
memory? Since the former contents are of no importance, what
difference does it make if you get "old" or "new" space?


One can easily envision a data structure that needs to be (relatively
expensively) rebuilt if moved, but that can extended in a
straight-forward fashion. The OP would like to avoid the pointless
copying of the existing data if he's only going to have to rebuild it
anyway, but be able to avoid the rebuild if an "extend" is possible.

Another possiblity is that he knows that he just accessed the old buffer
(and so a good portion is likely to be sitting in the cache) -- if he
switches to a new buffer, he'll take a bunch of cache hits.


An object large enough to have significant pieces paged out may also be
easier/faster to recreate than copy.

A better solution may be to break down what's likely a large and
complex data structure into smaller pieces most of which could avoid
needing to be extended as things grow thus eliminating the need to ever
move them).

Agreed; if the problem can be restructured in this way, this would be a
better solution

--
poncho




.



Relevant Pages

  • Re: realloc but not copy
    ... reuse if possible or allocate in a different part of RAM if resizing is ... but be able to avoid the rebuild if an "extend" is possible. ...
    (comp.lang.c)
  • Re: realloc but not copy
    ... reuse if possible or allocate in a different part of RAM if resizing is ... but be able to avoid the rebuild if an "extend" is possible. ...
    (comp.lang.c)
  • Re: NTFS block allocation policy (how does it work?)
    ... NTFS zeros the file whenever you extend it. ... So, if you pre-allocate the file, you only pay for the zeroing 1 time. ... Is there a way to allocate the file without incurring the overhead ...
    (microsoft.public.development.device.drivers)
  • Re: NTFS block allocation policy (how does it work?)
    ... NTFS zeros the file whenever you extend it. ... So, if you pre-allocate the file, you only pay for the zeroing 1 time. ... Is there a way to allocate the file without incurring the overhead ...
    (microsoft.public.windowsxp.embedded)
  • Re: NTFS block allocation policy (how does it work?)
    ... NTFS zeros the file whenever you extend it. ... So, if you pre-allocate the file, you only pay for the zeroing 1 time. ... Is there a way to allocate the file without incurring the overhead ...
    (microsoft.public.windows.file_system)

Loading