I saw some code related to realloc() on some sites as below.
int *p = (int *)malloc(sizeof(int) * 10);
p = (int *)realloc(p, 100);
But as the standard says, if realloc fails, the original block is left untouched and it returns NULL.
So if realloc fails, from above example, we will lose the ability to free p. Can any one please let me know is it good coding practice to assign the address returned by realloc() to the same pointer?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…