Reduction with realloc

I came across this small piece of code in this question and wanted to know

Can the realloc() function move a block of memory to another location when the specified memory space is reduced?

 int * a = malloc( 10*sizeof(int) ); int * b = realloc( a, 5*sizeof(int) ); 

If possible, under what conditions can I expect b have an address different from the address in a ?

+6
c realloc dynamic-memory-allocation
source share
1 answer

For realloc it is possible to move memory on any call. True, in many implementations, compression will simply change the reserved size on the heap and will not move memory. However, in a heap that is optimized for low fragmentation, it can choose to move memory to a more appropriate location.

Do not rely on realloc storing memory in one place for any operation.

+12
source share

All Articles