J
Jef Driesen
Hi,
Is there any difference in increasing the size of a dynamically
allocated buffer with realloc() or malloc()'ing a new buffer and
memcpy() the old data?
I can imagine realloc() can be more efficient than a malloc+memcpy
solution in some cases. For instance when there is enough free space
available immediately after the existing memory block, there is probably
no need to malloc() a new buffer and copy the data over. I am aware that
this is likely implementation dependent, but is there an advantage over
using realloc for typical implementations/platforms?
One of the reasons I'm asking this, is that sometimes I need to increase
the buffer at its start. If implemented with realloc(), I would need to
do an additional memmove() to move the existing data to the end. Thus in
that case the data needs to be moved anyway, and I'm not sure what is
more efficient in this situation.
Jef
Is there any difference in increasing the size of a dynamically
allocated buffer with realloc() or malloc()'ing a new buffer and
memcpy() the old data?
I can imagine realloc() can be more efficient than a malloc+memcpy
solution in some cases. For instance when there is enough free space
available immediately after the existing memory block, there is probably
no need to malloc() a new buffer and copy the data over. I am aware that
this is likely implementation dependent, but is there an advantage over
using realloc for typical implementations/platforms?
One of the reasons I'm asking this, is that sometimes I need to increase
the buffer at its start. If implemented with realloc(), I would need to
do an additional memmove() to move the existing data to the end. Thus in
that case the data needs to be moved anyway, and I'm not sure what is
more efficient in this situation.
Jef