This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: heap fragmentation & operator new
- From: Eljay Love-Jensen <eljay at adobe dot com>
- To: Burlen Loring <burlen dot loring at kitware dot com>, Andrew Haley <aph at redhat dot com>
- Cc: GCC-help <gcc-help at gcc dot gnu dot org>
- Date: Mon, 23 Jun 2008 08:41:51 -0500
- Subject: Re: heap fragmentation & operator new
Hi Burlen,
> Using malloc/realloc/free gives a performance advantage over new/delete
> since realloc need not move the block if it can resize inplace.
C++ STL's std::vector has that same advantage.
> I was exploring the new/delete avenue because it would seem cleaner to
> me to stick with C++ mechanism in a C++ code...
For what you are trying to do, and for the resizing (realloc) needs you
cited, std::vector is the appropriate C++ mechanism. Rather than new[] /
delete[].
But also note that a class can have an array pointer allocated by
std::malloc, deallocated by std::free, and reallocated (if/when necessary)
by std::realloc instead of using std::vector. Wrapping that in a class
provides a convenient spot to encapsulate that behavior. High cohesion, low
coupling.
I'd use std::vector, but if your team's expertise is in using the
malloc/realloc/free facilities it may be more expedient to encapsulate that
part of the array management in a class.
(Note: you'd be re-inventing the std::vector wheel for your particular data
type of interest. But that can be the Right Thing To Do depending on the
circumstances.)
Sincerely,
--Eljay