Resizing buffer using realloc?

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.

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.

Tnanks for the explanation! – mawia Apr 28 '09 at 21:01.

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.

– Jonathan Leffler Apr 26 '09 at 13:40 thanks for the explanation! – mawia Apr 28 '09 at 21:02.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions