我们可以肯定地说 realloc 的结果不会与原始指针互为别名吗?
Can we safely say that the result of realloc does not alias with the original pointer?
假设我们有:
char *a = malloc(sizeof(char*));
char *b = realloc(a,sizeof(char*));
我们可以肯定地说 b
不与 a
互为别名吗? realloc
参考页说
The original pointer ptr is invalidated and any access to it is
undefined behavior (even if reallocation was in-place).
所以我可以将 b
标记为非别名 a
因为我们不能再合法访问 a
了吗?然而,这可能会导致有问题的优化,其中将消除以下分支:
if (a == b)
something..
根据我的理解,a == b
本身的比较是 UB 那么这在技术上是正确的优化吗?
Based on my understanding, the comparison of a == b
itself would be UB..
嗯,是的。
关于"why"部分,如, after a pointer has been free()
d, the object it points to reaches the end of it's lifetime, thus making the pointer value indeterminate. So, any further use (read) of the pointer itself would be unspecified behavior at best and any attempted usage of the address pointed to by it, would invoke undefined behavior所述。
因此,从技术上讲,您期望的优化是正确的,并且 必须 才能使代码表现出定义的行为,因为代码一开始就不正确。不要指望编译器更正你的代码,它可能不会。
也就是说,关于
Can we safely say that "b" does not alias with "a"?
我不太清楚你在这里使用 别名 的依据是什么,但可以肯定的是,官方措辞来自 C11
,第 7.22 章.3.5,
P2:
The realloc
function deallocates the old object pointed to by ptr
and returns a
pointer to a new object that has the size specified by size
. 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. [...]
P3:
[...] If ptr
is a null pointer, the realloc
function behaves like the malloc
function for the
specified size
. Otherwise, if ptr
does not match a pointer earlier returned by a memory
management function, or if the space has been deallocated by a call to the free
or
realloc
function, the behavior is undefined. If memory for the new object cannot be
allocated, the old object is not deallocated and its value is unchanged.
并且,P4:
The realloc
function returns a pointer to the new object (which may have the same
value as a pointer to the old object), or a null pointer if the new object could not be
allocated.
释放后,a
的值不确定。
n1570-§6.2.3 (p2):
[...] If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
如果此不确定值成为陷阱表示,则比较 a == b
将导致未定义的行为。
注意当指针被传递给free
时,指针指向的对象的生命周期结束。
进一步阅读:
1. Why isn't a pointer null after calling free?
2. A dangling pointer is indeterminate.
假设我们有:
char *a = malloc(sizeof(char*));
char *b = realloc(a,sizeof(char*));
我们可以肯定地说 b
不与 a
互为别名吗? realloc
参考页说
The original pointer ptr is invalidated and any access to it is undefined behavior (even if reallocation was in-place).
所以我可以将 b
标记为非别名 a
因为我们不能再合法访问 a
了吗?然而,这可能会导致有问题的优化,其中将消除以下分支:
if (a == b)
something..
根据我的理解,a == b
本身的比较是 UB 那么这在技术上是正确的优化吗?
Based on my understanding, the comparison of
a == b
itself would be UB..
嗯,是的。
关于"why"部分,如free()
d, the object it points to reaches the end of it's lifetime, thus making the pointer value indeterminate. So, any further use (read) of the pointer itself would be unspecified behavior at best and any attempted usage of the address pointed to by it, would invoke undefined behavior所述。
因此,从技术上讲,您期望的优化是正确的,并且 必须 才能使代码表现出定义的行为,因为代码一开始就不正确。不要指望编译器更正你的代码,它可能不会。
也就是说,关于
Can we safely say that "b" does not alias with "a"?
我不太清楚你在这里使用 别名 的依据是什么,但可以肯定的是,官方措辞来自 C11
,第 7.22 章.3.5,
P2:
The
realloc
function deallocates the old object pointed to byptr
and returns a pointer to a new object that has the size specified bysize
. 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. [...]
P3:
[...] If
ptr
is a null pointer, therealloc
function behaves like themalloc
function for the specifiedsize
. Otherwise, ifptr
does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to thefree
orrealloc
function, the behavior is undefined. If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.
并且,P4:
The
realloc
function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.
释放后,a
的值不确定。
n1570-§6.2.3 (p2):
[...] If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.
如果此不确定值成为陷阱表示,则比较 a == b
将导致未定义的行为。
注意当指针被传递给free
时,指针指向的对象的生命周期结束。
进一步阅读:
1. Why isn't a pointer null after calling free?
2. A dangling pointer is indeterminate.