B
bintom
Is there any equivalent of C's realloc() in C++?
Bintom
Bintom
bintom said:Is there any equivalent of C's realloc() in C++?
C++ inherits realloc() from C
/Leigh
bintom said:OK, realloc() may be used, but then there would be no need for the
'new' operator when malloc() was already available.
Is there any equivalent of C's realloc() in C++?
James Kanze said:std::vector<>::resize. With the advantage, of course, that it
actually works, regardless of the object type.
Juha said:Note that, depending on the underlying implementation and on the
current memory allocation layout, realloc() may in some cases be
much more efficient than resizing a vector because it may be able
to skip copying the existing data (by simply expanding the existing
memory block, if there is room).
There is no such functionality with 'new' in C++.
There is such functionality with `std::vector<>`. The capacity of a vector
is not required to be less than any specified number, as far as I know. So
if the capacity of a vector is N > size(), then resizing to M <= N does
not
require copy constructor calls either
"Johannes Schaub (litb)" <[email protected]
And a good quality implementation certainly has special access to the memory
manager to ask 'expand in place or leave alone', and not limited to call
actual resize() like a handwritten array class would.
Howard Hinnant said:The first difficult task is just convincing C++ programmers that
vector::resize (as implementable today) actually is sufficiently sub-
optimal to matter.
I'd say that the practical situations where realloc() can increase the
size of the allocated block in-situ (ie. simply by expanding the existing
allocated block, thus being able to skip copying any data) are really
rare. Perhaps if you need to call realloc() many times in succession,
and the memory block is (or ends up being, before the last realloc()
call) at the end of the current heap.
If such a situation is an essential part of some program, it might be
worth considering using std::deque instead (which expands without the
need to copy data around).
std::vector<>::resize. With the advantage, of course, that it
actually works, regardless of the object type.
Howard said:std::deque is a very interesting data structure. For all but the
rarest of applications I consider it suboptimal. A circular buffer
that is capable of geometric reallocation when size exceeds capacity
nearly always beats an array of arrays in performance. It is only
when you need all of push_front, push_back and pointer/reference
stability that std::deque really shines. A circular buffer gives you
push_front/back without pointer/reference stability, and would also
benefit from inplace expansion.
But still beats a deque even without inplace expansion if the
pointer/reference stability isn't required (in my experience).
Vladimir Jovic said:They are not doing the same thing. new is calling the constructor, while
malloc() doesn't
Howard Hinnant wrote:
You may still need to move most elements when doing inplace expansion
(just checking that I understand the data structure you describe).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.