473,466 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Memory allocation

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
2 1832
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
4
by: PaulR | last post by:
Hi, We have a Server running SLES 8 and 3GB memory, with 1 DB2 instance and 2 active Databases. General info... DB2level = "DB2 v8.1.0.72", "s040914", "MI00086", and FixPak "7" uname -a =...
74
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique...
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
34
by: jacob navia | last post by:
Suppose that you have a module that always allocates memory without ever releasing it because the guy that wrote it was lazy, as lazy as me. Now, you want to reuse it in a loop. What do you do?...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
66
by: karthikbalaguru | last post by:
Hi, Will 'free' return the memory Immediately to the OS ? Thx in advans, Karthik Balaguru
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.