By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Memory allocation

P: n/a
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..what
happens to this chunk of memory? Is it freed by the operating system?

Sep 20 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
krish wrote:
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..what
happens to this chunk of memory? Is it freed by the operating system?
Your runtime library frees it. The reason you _don't_ free it is the
realloc() function might opt to grow your pointer's pointed-to memory
in-place, by claiming more of the unclaimed storage after it. So your own
code should not worry whether the pointer is really changed or not.

Now use std::vector<>, without an overwhelming and degenerate reason to use
realloc()!

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Sep 20 '06 #2

P: n/a
On 20 Sep 2006 03:35:03 -0700, "krish" <kr************@gmail.com>
wrote in comp.lang.c++:
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().

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.