What if null and size 0 are passed to realloc ()?

Is the implementation of behavior defined? If NULL and size == 0 are passed to realloc():

int main(void)
{
    int *ptr = NULL;

    ptr = realloc(ptr, 0);

    if(ptr == NULL)
    {
        printf("realloc fails.\n");
        goto Exit;
    }

    printf("Happy Scenario.\n");

Exit:
    printf("Inside goto.\n");

return 0;
}

The above code should print "realloc fail", right? But is that not so? I read somewhere that this call realloccan also return NULL. When does this happen?

+4
source share
3 answers

This behavior is determined by the implementation.

From Standard C :

Section 7.22.3.5 ( realloc):

3 ptr - , realloc malloc . , ptr , , free realloc, undefined. , .

, realloc(NULL, 0) malloc(0)

7.22.3.4 (malloc):

2 malloc , size .

3 malloc , .

, , 0 .

Linux :

malloc() . . 0, malloc() NULL, , free().

, , NULL.

, MSDN :

0, malloc . malloc, .

, MSVC NULL.

+7

realloc (3) doc:

ptr NULL, malloc ()

malloc (3) doc:

0, malloc() NULL, , free().

, , , , , .

+11

realloc(NULL, size);

malloc(size);

malloc() , 0 , , . , . " "; NULL, , , . free().

+6

Source: https://habr.com/ru/post/1670102/


All Articles