在阅读有关内容realloc()
时,我偶然发现了一些我需要澄清而不是忽略的疑问。您的答案非常受欢迎。为了清楚起见,我已将它们放在编号列表中。请不要介意这个问题的长度。
1)在使用realloc()
时,如果内存块及其内容被移动到一个新位置,原始地址是否会像我们调用 free() 一样被释放cplusplusreference
?我已经从about中阅读了以下内容realloc
,但尽管它们接近建议在这种情况下原始内存块确实会被释放,但我需要您的确认。
->C90 (C++98)C99/C11 (C++11)
Otherwise, if size is zero, the memory previously allocated at ptr is deallocated as if a call to free was made, and a null pointer is returned.
->If the function fails to allocate the requested block of memory, a null pointer is returned, and the memory block pointed to by argument ptr is not deallocated (it is still
valid, and with its contents unchanged).
2) 这是引起问题的另一行:"If the new size is larger, the value of the newly allocated portion is indeterminate."
。好吧,这就是我想知道的
i)是否允许写入新分配的部分?
ii)新分配的部分是否使用垃圾值填充?
3)最后,如果传递一个数组对象到描述为realloc(
char*
"Pointer to a memory block previously allocated with malloc, calloc or realloc.
free()
free()
"If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior."