"realloc" without copying

48 views
Skip to first unread message

Frederick Gotham

unread,
Oct 24, 2006, 12:22:32 PM10/24/06
to

It seems there is more than one application for a "realloc" function which
doesn't copy over the original data if the memory chunk should be relocated.

Any chance of adding such a function to the language?

--

Frederick Gotham

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Frederick Gotham

unread,
Oct 24, 2006, 12:39:46 PM10/24/06
to
Frederick Gotham posted:

>
>
> It seems there is more than one application for a "realloc" function
> which doesn't copy over the original data if the memory chunk should be
> relocated.
>
> Any chance of adding such a function to the language?


And perhaps even a "realloc_check" function which would check whether it is
necessary to reallocate the buffer?

Howard Hinnant

unread,
Oct 24, 2006, 2:32:07 PM10/24/06
to
In article <har%g.15176$j7.3...@news.indigo.ie>,
fgot...@SPAM.com (Frederick Gotham) wrote:

> Frederick Gotham posted:
>
> >
> >
> > It seems there is more than one application for a "realloc" function
> > which doesn't copy over the original data if the memory chunk should be
> > relocated.
> >
> > Any chance of adding such a function to the language?
>
>
> And perhaps even a "realloc_check" function which would check whether it is
> necessary to reallocate the buffer?

Your timing is impeccable. WG14 discussed this yesterday:

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1085.htm

I'm not certain what their decision will be on it. But I believe some
support has been voiced for at least a subset of this proposal.

-Howard

Andrei Polushin

unread,
Oct 24, 2006, 3:36:54 PM10/24/06
to
Frederick Gotham wrote:
> It seems there is more than one application for a "realloc" function
> which doesn't copy over the original data if the memory chunk should be
> relocated.
>
> Any chance of adding such a function to the language?
>
> And perhaps even a "realloc_check" function which would check whether it
> is necessary to reallocate the buffer?

Note that "reallocate" is not identical to "relocate". It may relocate
to prevent memory fragmentation or just for fun :)

"realloc_check" is simply harmful, because you may get "true" for the
first call, and "false" for the second call (due to multithreading).

Isn't it better to customize realloc with move handler?

typedef void(*move_handler)(void* to, const void* from, size_t n);

void* _expand_fit(void* p, size_t new_size); // find larger block
void* _better_fit(void* p, size_t new_size); // find better block

void* realloc(void* p, size_t old_size, size_t new_size,
move_handler move)
{
if (old_size < new_size) {
void* r = _expand_fit(p, new_size);
if (r != p) {
move(r, p, old_size);
free(p);
}
return r;
} else {
void* r = _better_fit(p, new_size);
if (r != p) {
move(r, p, new_size);
free(p);
}
return r;
}
}

size_t _mem_size(void* p); // get block size

// classic realloc:
void* realloc(void* p, size_t new_size)
{
return realloc(p, _mem_size(p), new_size,
(move_handler)memmove);
}

--
Andrei Polushin

Reply all
Reply to author
Forward
0 new messages