Re: realloc but not copy
- From: "Scott Fluhrer" <sfluhrer@xxxxxxxxxxxxx>
- Date: Tue, 24 Oct 2006 09:40:56 -0400
<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
.
- References:
- realloc but not copy
- From: ivan . leben
- Re: realloc but not copy
- From: Eric Sosman
- Re: realloc but not copy
- From: robertwessel2@xxxxxxxxx
- realloc but not copy
- Prev by Date: Re: how to allocate memory to a member dynamically
- Next by Date: Re: how to allocate memory to a member dynamically
- Previous by thread: Re: realloc but not copy
- Next by thread: Re: realloc but not copy
- Index(es):
Relevant Pages
|
Loading