Thursday 11 April 2013

Question: Why doesn't C++ have an equivalent to realloc()?


Question: Why doesn't C++ have an equivalent to realloc()?

 Answer: If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember that contrary to naive expectations, realloc() occationally does copy its argument array.
In C++, a better way of dealing with reallocation is to use a standard library container, such as vector, and let it grow naturally.

No comments:

Post a Comment