Search code examples
crealloc

What are the contains of the extended memory obtained using realloc?


When I use realloc to re-size the memory which I used calloc earlier to initialize (an array), is it still remains 0 in a whole buffer? or the new part is not initialized?


Solution

  • Considering you're increasing the size of the allocated memory, the extended memory region will have indeterminate values.

    Quoting C11, chapter §7.22.3.5, (emphasis mine)

    [...] The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes. Any bytes in the new object beyond the size of the old object have indeterminate values.