4

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
mawia
  • 9,169
  • 14
  • 48
  • 57

3 Answers3

16

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
  • 74,600
  • 47
  • 176
  • 233
11

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

Addr       A                   B
     +------------+      +------------+
1000 | your space |      | your space |
     +------------+      +------------+
2000 | free space |      | used space |          
     |            |      +------------+
3000 |            |      | free space |
     |            |      |            |
4000 |            |      |            |
     +------------+      +------------+

In both situations, you have 1000 bytes allocated at address 1000. 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 2000 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:

Addr       A                   B
     +------------+      +------------+
1000 | your space |      | free space |
     |            |      +------------+
2000 |            |      | used space |
     +------------+      +------------+
3000 | free space |      | your space |
     |            |      |            |
4000 |            |      |            |
     +------------+      +------------+

For situation B, the allocator finds a block (at 3000) that is big enough for your desired expansion, and copies the contents of your current block (at 1000) 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
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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 Apr 26 '09 at 13:40
2

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
i_am_jorf
  • 53,608
  • 15
  • 131
  • 222