when we call realloc() the previous block of memory assigned through
calling malloc is copied into a new location and the re-allocation of
memory is done but the previous block of memory still remains.
Not necessarily. If there is sufficient room on the free store
directly following the original block, realloc() may just expand the
block in place, and not need to move or copy anything. And if it is
making the block smaller, it may leave it in the same place.
.what
happens to this chunk of memory? Is it freed by the operating system?
Actually, the C++ language does not specify that. The C++ language
standard is based on the C language standard as of 1995. Due to an
oversight, the original C standard does not say what happens to the
original block of memory if realloc() does need to move the contents.
This was corrected in the 1999 version of the C standard, which says
that if a successful realloc() moves the contents to a new block, the
old block is freed. But technically, this is not normative to the C++
standard.
In the real world, I am sure all C++ and C compilers, no matter what
version of their respective standard they conform to, free the old
block when realloc() moves the contents to a new one. The same as if
you called free() on the block.
That does not mean that the memory is returned to the operating
system, and in fact most implementations do not do that, merely keep
it available for a further call to malloc(), calloc(), or realloc().