Is realloc guaranteed to be in-place when the buffer is shrinking?

前端 未结 5 1130
遇见更好的自我
遇见更好的自我 2021-01-04 09:33

Are there any guarantees that realloc() will always shrink a buffer in-place?? So that the following:

new_ptr = (data_type *) realloc(old_ptr, new_size * siz         


        
5条回答
  •  遥遥无期
    2021-01-04 10:33

    Some allocators use a "bucketizing" strategy where allocations sized from, say, 2^3 through 2^4, go to the same allocation bucket. This tends to prevent extreme cases of memory fragmentation where many small allocations spread across the heap prevent large allocations from succeeding. Obviously, in such a heap manager, reducing the size of an allocation could force it to a different bucket.

提交回复
热议问题