mawia
mawia

Reputation: 9349

resizing buffer using realloc

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.

Upvotes: 4

Views: 4145

Answers (3)

paxdiablo
paxdiablo

Reputation: 881583

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.

Upvotes: 11

i_am_jorf
i_am_jorf

Reputation: 54600

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.

Upvotes: 2

Naveen
Naveen

Reputation: 73443

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

Upvotes: 16

Related Questions