tags:

views:

349

answers:

3

If the area pointed to was moved, a free(ptr) is done.

Can you please explain the above line about realloc()? This line is from a man page for calloc, malloc, realloc and free.

+10  A: 

I think this explains it better:

If sufficient space does not exist to expand the current block in its current location, a new block of the size for size is allocated, and existing data is copied from the old block to the beginning of the new block. The old block is freed, and the function returns a pointer to the new block.

Reference taken from realloc in C

Naveen
tnanks for the explanation!
mawia
+2  A: 

You can't always just grow the memory area in situ. There may not be room in the heap. So instead of growing it, it will allocate a completely new memory block and free the old memory.

jeffamaphone
+5  A: 

Let's say you have the following heap layouts. This is a simplified memory allocator where no space is taken up in the heap by control information

           A                   B
     +------------+      +------------+
1024 | your space |      | your space |
     +------------+      +------------+
2048 | free space |      | used space |          
     |            |      +------------+
3072 |            |      | free space |
     |            |      |            |
4096 |            |      |            |
     +------------+      +------------+

In both situations, you have 1024 bytes allocated at address 1024. However, in situation B, this is immediately followed by memory allocated for some other purpose.

Let's examine what happens when you want to reallocate your memory to 2048 bytes.

In situation A, this is easy, it just expands your allocation as per the diagram below.

But, in situation B, it's not so easy. The memory immediately following your block is in use so there isn't enough room to just expand your allocation, and you need consecutive memory. Here's the end position for the two situations:

           A                   B
     +------------+      +------------+
1024 | your space |      | free space |
     |            |      +------------+
2048 |            |      | used space |
     +------------+      +------------+
3072 | free space |      | your space |
     |            |      |            |
4096 |            |      |            |
     +------------+      +------------+

For situation B, the allocator finds a block (3192) that is big enough for your desired expansion, and copies the contents of your current block (1024) to it. Then it gives you the address of this new block and frees the old block since that is no longer required by you. That's what the phrase in your question means.

This action of moving buffers around depends on the memory allocation strategy but, generally, a buffer won't be moved (it's often expensive since it involves a mass memory copy) if either:

  • there is free space after it that, along with the current space, can satisfy the reallocation; or
  • you're reducing the size.
paxdiablo
Do you need to clarify that the "someone else" who has used the extra space is 'another part of your program that allocated space' as opposed to 'another process run by another user'?
Jonathan Leffler
thanks for the explanation!
mawia