This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]