简体   繁体   English

关于realloc功能的困惑

[英]Confusion about realloc function

I read about dynamic memory allocation in C using this reference. 我使用参考读了C中的动态内存分配。

That document Say's : 那个文件Say's:

realloc() should only be used for dynamically allocated memory. realloc()只应用于动态分配的内存。 If the memory is not dynamically allocated, then behavior is undefined. 如果未动态分配内存,则行为未定义。

If we use realloc() something like this: 如果我们使用这样的realloc()

int main()
{
    int *ptr;
    int *ptr_new = (int *)realloc(ptr, sizeof(int));

    return 0;
}

According to that reference, this program is undefined because pointer ptr not allocated dynamically. 根据该引用,该程序未定义,因为指针ptr未动态分配。

But, If I use something like: 但是,如果我使用类似的东西:

int main()
{
    int *ptr = NULL;
    int *ptr_new = (int *)realloc(ptr, sizeof(int));

    return 0;
}

Is it also undefined behavior according to that reference? 根据该参考,它是否也是未定义的行为?

I thing second case does not invoked undefined behaviour. 我的第二种情况不会调用未定义的行为。 Am I right? 我对吗?

The first case has undefined behavior, and the second doesn't. 第一种情况有未定义的行为,第二种情况没有。 In the first case, the value of ptr is indeterminate. 在第一种情况下, ptr的值是不确定的。 So passing that value to realloc or any function, is undefined by itself. 因此,将该值传递给realloc或任何函数,本身是不确定的。

On the other hand, since realloc has well defined behavior when passed a null pointer value (it's just like calling malloc ) 1 , the second piece of code is perfectly legitimate (other than the fact you don't free anything). 另一方面,由于realloc在传递空指针值时具有良好定义的行为(它就像调用malloc1 ,第二段代码完全合法(除了你没有free任何东西的事实)。


1 7.22.3.5 The realloc function / p3 1 7.22.3.5 realloc函数/ p3

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. 如果ptr是空指针,则realloc函数的行为类似于指定大小的malloc函数。

In the first case the program almost sure will finish by segmentation fault as the linked lists that are created in the heap to find segments are not coherent, in the second case you call the realloc with the NULL first parameter, which means, is a call equivalent to malloc(size) 在第一种情况下,程序几乎肯定将通过segmentation fault完成,因为在堆中创建的链接列表以查找段不一致,在第二种情况下,您使用NULL第一个参数调用realloc,这意味着,是一个调用相当于malloc(size)

man realloc says: man realloc说:

   void *malloc(size_t size); 
   void *realloc(void *ptr, size_t size);

If ptr is NULL, then the call is equivalent to malloc(size), for all values of size 如果ptr为NULL,则对于所有size值,调用等效于malloc(size)

The only authorative reference is the standard document. 唯一的授权参考是标准文档。 n1570 (the latest C11 standard) has the following to say: n1570 (最新的C11标准)有如下说法:

§7.22.3.5 The realloc function, p3 : §7.22.3.5的realloc功能,P3:

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. 如果ptr是空指针,则realloc函数的行为类似于指定大小的malloc函数。 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. 否则,如果ptr与先前由内存管理函数返回的指针不匹配,或者如果通过调用freerealloc函数释放了空间,则行为未定义。 [...] [...]

So, your second example is well-defined. 所以,你的第二个例子是明确定义的。

  1. The first case is obviously undefined behavior because we don't know where the ptr is pointing or what the ptr is holding at that time. 第一种情况显然是未定义的行为,因为我们不知道ptr指向的位置或者ptr当时持有的内容。 And c standard says that 7.20.3.4.2 The realloc function 并且c标准说7.20.3.4.2 realloc函数

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. realloc函数释放ptr指向的旧对象,并返回指向具有size指定大小的新对象的指针。

So first case is Undefined behavior. 所以第一种情况是未定义的行为。

  1. In second case compiler knows what ptr has so it is valid but realloc() will act as malloc() according to 7.20.3.4.3 The realloc function 在第二种情况下,编译器知道ptr具有什么,因此它是有效的,但realloc()将根据7.20.3.4.3 realloc函数充当malloc()

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. 如果ptr是空指针,则realloc函数的行为类似于指定大小的malloc函数。

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

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