简体   繁体   中英

Confusion about realloc function

I read about dynamic memory allocation in C using this reference.

That document Say's :

realloc() should only be used for dynamically allocated memory. If the memory is not dynamically allocated, then behavior is undefined.

If we use realloc() something like this:

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.

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. So passing that value to realloc or any function, is undefined by itself.

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).


1 7.22.3.5 The realloc function / p3

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.

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)

man realloc says:

   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

The only authorative reference is the standard document. n1570 (the latest C11 standard) has the following to say:

§7.22.3.5 The realloc function, 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. [...]

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. And c standard says that 7.20.3.4.2 The realloc function

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.

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

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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