简体   繁体   English

使用realloc获得的扩展内存包含了什么?

[英]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? 当我使用realloc来重新调整我之前使用calloc初始化(一个数组)的内存时,它是否仍然在整个缓冲区中保持为0? or the new part is not initialized? 或新部分未初始化?

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 ) 引用C11 ,章节§7.22.3.5,( 强调我的

[...] 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. 新对象中超出旧对象大小的任何字节都具有不确定的值。

No, unfortunately realloc will not initialize the "new" part that is allocated. 不,遗憾的是realloc不会初始化分配的“新”部分。 So you have to do that yourself. 所以你必须自己做。 The "old" part will keep its value, though, so if you didn't change that, all your 0 s will still be there. 但是,“旧”部分将保留其价值,所以如果你没有改变它,你的所有0都将保持在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM