C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

"realloc" without copying

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Frederick Gotham
Guest





PostPosted: Tue Oct 24, 2006 9:22 pm    Post subject: "realloc" without copying Reply with quote



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-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Andrei Polushin
Guest





PostPosted: Tue Oct 24, 2006 9:22 pm    Post subject: Re: "realloc" without copying Reply with quote



Frederick Gotham wrote:
Quote:
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

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





PostPosted: Tue Oct 24, 2006 9:39 pm    Post subject: Re: "realloc" without copying Reply with quote



Frederick Gotham posted:

Quote:


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?

--

Frederick Gotham

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





PostPosted: Tue Oct 24, 2006 11:32 pm    Post subject: Re: "realloc" without copying Reply with quote

In article <har%g.15176$j7.332517 (AT) news (DOT) indigo.ie>,
fgothamNO (AT) SPAM (DOT) com (Frederick Gotham) wrote:

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

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.