* Michael Kerrisk: > Correct usage of realloc() (and reallocarray()) > Since, on the one hand, realloc() (and reallocarray()) may move > the block of memory, and on the other, it may return NULL on fail‐ > ure and leave the memory contents and location unchanged, correct > usage is something like the following: > > void *ptr, *nptr; > ptr = malloc(origsize); > ... > /* In the following, we presume 'newsize' is not 0. > (If 'newsize' is zero, realloc() may return NULL, > and that is not an error.) */ > > nptr = realloc(ptr, newsize); > if (nptr == NULL) { > /* Handle error; the block pointed to by 'ptr' is > still usable. */ > } else { > /* realloc() succeeded; update 'ptr' to point to > the (possibly moved) block. */ > ptr = nptr; > } Maybe add this to the else branch: “The original value of ptr itself and the memory it pointed to are invalid at this point.” That is, the somewhat common idiom of adjusting internal pointers in the allocation to point to the new allocation is invalid. Thanks, Florian