The realloc(3) – corner cases

Consider the following code snippet:

void *ptr, *newptr;ptr = calloc(100, sizeof(char)); // error checking code not shown herenewptr = realloc(NULL, 150);

The pointer passed to realloc is NULL? The library treats this as equivalent to a new allocation – malloc(150); and all the implications of the malloc(3) That's it.

Now, consider the following code snippet:

void *ptr, *newptr;ptr = calloc(100, sizeof(char)); // error checking code not shown herenewptr = realloc(ptr, 0);

The size parameter passed to realloc is 0? The library treats this as equivalent to free(ptr). That's it.

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.