REALLOC AND FREE

V

vivek

Hi,

If i allocate some memory using malloc(bigger size memory) and then
reallocate the same pointer returned by malloc(for a smaller size
memory), what happens to the remainng memory??? When is it
available???

Is the user free to do pointer manipulation to access the remaining
memory???

Then after i use realloc, when i free the pointer, how does it
react???

Does it release the bigger size memory???

If no, what happens to the remaining memory???
 
R

Richard Heathfield

vivek said:
Hi,

If i allocate some memory using malloc(bigger size memory) and then
reallocate the same pointer returned by malloc(for a smaller size
memory), what happens to the remainng memory??? When is it
available???

Given a pointer to the first byte in a block of memory successfully
allocated by malloc, you can pass that pointer to realloc, which may be
able to change the size of the allocated block. If realloc returns a null
pointer, the change failed. Otherwise, the pointer value it returns should
be used *instead of* your original pointer, which is no longer valid. If
the block shrank, as in your question, the amount by which it shrank no
longer belongs to your program and must not be used.

It may be that the pointer returned by realloc happens to be the same as
the original pointer (that is, realloc was able to do the resizing "in
place"), or it may be that it isn't (because realloc chose to reposition
the block, in which case it will copy the contents of the old block to the
new block, up to the size of the smaller of the two).

Either way, you mustn't use the old pointer and you mustn't use the memory
you no longer own.

So here's how we normally do it, for some object type T, such that 'new'
and 'old' are both of type T *, and 'old' points to the first byte in a
block successfully allocated with malloc. n is the new number of objects
we wish to allocate:

T *new = realloc(old, n * sizeof *old);
if(new != NULL)
{
old = new;
new = NULL;
}
else
{
handle the failure in some appropriate way
}
Is the user free to do pointer manipulation to access the remaining
memory???

If you mean the memory that the program used to own and no longer owns, the
answer is no - the behaviour is undefined if you do that.
Then after i use realloc, when i free the pointer, how does it
react???

As you would expect. It marks the allocated block as being available for
further allocation, which means that it's no longer available to you
(except by calling malloc again, and you might get that block back or you
might not, and it might have the same data in it as when you released it
or it might not). In general, if you still need the memory, don't free it.
If you free it, don't expect to see it again.
Does it release the bigger size memory???

No, because that was already handled during the realloc call.
If no, what happens to the remaining memory???

As far as your program is concerned, it vanishes. If you want the same
block back, don't free it in the first place. (But it isn't a memory
"leak" - the block becomes available for further allocation.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top