can use realloc to extend a memory allocated with new?and
if not why?


The realloc function is part of the C dynamic memory handling functions and goes with malloc, free, calloc.

The equivalent C++ support in the form of new, delete, new[] and delete[] do not just allocate and de-allocate memory (as do malloc/free etc.) they also create and destroy the objects allocated or released. That is new and new [] obtain memory for the object or objects to be created then calls a constructor for each object to create each object and delete and delete [] call the destructor for each object to be destroyed and then releases any storage used.

Further more new, delete, new[] and delete[] are operators not functions. This means that like most C++ operators they can be overloaded for user defined types (classes), and even the global default versions may be replaced by user defined versions. Hence when using new and delete the standard C++ heap might not be used to obtain storage as some custom allocation scheme may be in use and the storage came from elsewhere via an overloaded or replaced operator.

Note that I mentioned the C++ heap. There is no reason this heap is the same as that used by the C functions malloc, free, realloc etc. although in many cases it will be. In fact the C++ standard states that:

For default new operator:
       "Executes a loop: Within the loop, the
        function first attempts to allocate
        the requested storage. Whether the
        attempt involves a call to the Standard
        C library function malloc is unspecified."

For default delete of non-null pointer:
       "Any other value of ptr shall be a value
        returned earlier by a call to the default
        operator new, which was not invalidated
        by an intervening call to operator
        delete(void*). For such a non-null value
        of ptr, reclaims storage allocated by the
        earlier call to the default operator new.

        Notes: It is unspecified under what conditions
        part or all of such reclaimed storage is
        allocated by a subsequent call to operator new
        or any of calloc, malloc, or realloc, declared
        in <cstdlib>."

For the C functions:
       "The functions calloc(), malloc(), and realloc()
        do not attempt to allocate storage by calling
        ::operator new().

        The function free() does not attempt to deallocate
        storage by calling ::operator delete()."

Note that when using operator delete the pointer operand to delete, if not null, has to have been previously acquired by a call to new, hence it cannot have been returned from realloc!

You will also note that the interaction between new and delete and malloc/free/calloc/realloc is carefully worded so that the C functions do NOT rely on the C++ operators and the C++ operators do not necessarily need to be implemented in terms of the C functions, although they can be!

See also Scott Meyers "Effective C++" book, specifically "Item 3: Prefer new and delete to malloc and free", which ends:

       "Given that malloc and free are ignorant of
        constructors and destructors and that mixing
        malloc/free with new/delete is more volatile
        than a fraternity rush party, you're best off
        sticking to an exclusive diet of news and
        deletes whenever you can"

As realloc is part of the malloc/free family I would extend these sentiments to include realloc.

I strongly recommend you read "Effective C++" and Scott Meyers other C++ titles (for starters...).  


All Answers

Answers by Expert:

Ask Experts


Ralph McArdell


I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.


My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.


©2017 About.com. All rights reserved.