访问指针传递给realloc [duplicate]

时间:2021-05-23 21:03:08

This question already has an answer here:

这个问题在这里已有答案:

#include <stdio.h>
#include <stdlib.h>

main()
{
    int *p = (int *)malloc(sizeof(int));
    int *q = (int *)realloc(p,sizeof(int));
    *p = 3;
    *q = 9;
    if (p == q)
        printf("%d %d", *p, *q);

}

when i run this it print 9 9 in GCC. Can someone explain the behavior of this code?

当我运行它时,它在GCC中打印9 9。有人可以解释这段代码的行为吗?

1 个解决方案

#1


6  

What does the standard say?

Going strictly by the standard, your program exhibits undefined behavior (C standard, 7.22.3.5):

严格遵循标准,您的程序表现出不确定的行为(C标准,7.22.3.5):

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. Any bytes in the new object beyond the size of the old object have indeterminate values.

realloc函数解除分配ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针。新对象的内容应与解除分配之前的旧对象的内容相同,直到新旧大小中的较小者为止。新对象中超出旧对象大小的任何字节都具有不确定的值。

The two objects involved in realloc are not the same - the new one is allocated, and you know its contents are the same as the old one, and the old one is destroyed. Your dereference of p is access to an object outside its lifetime, which is undefined.

realloc中涉及的两个对象不一样 - 分配了新对象,并且您知道其内容与旧内容相同,旧内容被销毁。您对p的解引用是访问其生命周期之外的对象,这是未定义的。

What (probably) happens in practice here?

realloc can move the memory area if necessary, but does not have to move the memory area.

如果需要,realloc可以移动内存区域,但不必移动内存区域。

In this situation, both allocations are of the same size (sizeof(int)). realloc very likely realizes this and doesn't move the memory area at all.

在这种情况下,两个分配都具有相同的大小(sizeof(int))。 realloc很可能意识到这一点并且根本不会移动内存区域。

As a result, p == q.

结果,p == q。

PS: "probably" because the behavior is undefined. Your compiler might just analyze the realloc code path and notice that the only legal way the program can work is if realloc doesn't move the pointer, and optimizes to q = p instead. You'd need to investigate the assembly generated to really find out.

PS:“可能”因为行为未定义。您的编译器可能只是分析realloc代码路径并注意到程序可以工作的唯一合法方式是realloc是否移动指针,而是优化为q = p。您需要调查生成的程序集才能真正找到答案。

#1


6  

What does the standard say?

Going strictly by the standard, your program exhibits undefined behavior (C standard, 7.22.3.5):

严格遵循标准,您的程序表现出不确定的行为(C标准,7.22.3.5):

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. Any bytes in the new object beyond the size of the old object have indeterminate values.

realloc函数解除分配ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针。新对象的内容应与解除分配之前的旧对象的内容相同,直到新旧大小中的较小者为止。新对象中超出旧对象大小的任何字节都具有不确定的值。

The two objects involved in realloc are not the same - the new one is allocated, and you know its contents are the same as the old one, and the old one is destroyed. Your dereference of p is access to an object outside its lifetime, which is undefined.

realloc中涉及的两个对象不一样 - 分配了新对象,并且您知道其内容与旧内容相同,旧内容被销毁。您对p的解引用是访问其生命周期之外的对象,这是未定义的。

What (probably) happens in practice here?

realloc can move the memory area if necessary, but does not have to move the memory area.

如果需要,realloc可以移动内存区域,但不必移动内存区域。

In this situation, both allocations are of the same size (sizeof(int)). realloc very likely realizes this and doesn't move the memory area at all.

在这种情况下,两个分配都具有相同的大小(sizeof(int))。 realloc很可能意识到这一点并且根本不会移动内存区域。

As a result, p == q.

结果,p == q。

PS: "probably" because the behavior is undefined. Your compiler might just analyze the realloc code path and notice that the only legal way the program can work is if realloc doesn't move the pointer, and optimizes to q = p instead. You'd need to investigate the assembly generated to really find out.

PS:“可能”因为行为未定义。您的编译器可能只是分析realloc代码路径并注意到程序可以工作的唯一合法方式是realloc是否移动指针,而是优化为q = p。您需要调查生成的程序集才能真正找到答案。