The book "C++ coding standards 101 Rules,Guidelines, and Best Practices" by Herb Sutter & Andrei Alexandrescu Item 5 says that
Give one entity one cohesive responsibility.
Focus on one thing at a time: Prefer to give each entity (variable, class, function,namespace, module, library) one well-defined responsibility. As an entity grows, its scope of responsibility naturally increases,but its responsibility should not diverge
The book also gives example of C's realloc() function.
In Standard C, realloc is an infamous example of bad design. It has to do too many things: allocate memory if passed NULL, free it if passed a zero size, reallocate it in place if it can, or move memory around if it cannot. It is not easily extensible. It is widely viewed as a short-sighted design failure.
yes, realloc() can be used to deallocate memory also as we know. see this also.
But my questions are:
1) Why it is badly designed? Why it is designed to perform more than single task?
2) Why it isn't extensible?
Thanks
Because checking whether memory can be allocated and then actually allocating it involve some common steps that would need to be re-taken (and thus slower) if done separately.
Furthermore, combining the free operation with the allocation operation could not only be faster, but it could also allow memory to be allocated that might otherwise not be available in a separate, contiguous location.
(Imagine allocating 768 MB of memory and then asking for 1 GB on a 1.5 GB machine...)
Because it was intended for C, not C++.