This is the way I've been taught to use realloc()
:
int *a = malloc(10);
a = realloc(a, 100); // Why do we do "a = .... ?"
if(a == NULL)
//Deal with problem.....
Isn't that redundant? Can't i just do something like this? :
if(realloc(a, 100) == NULL) //Deal with the problem
Same for other realloc examples i've found around, for example:
int *oldPtr = malloc(10);
int * newPtr = realloc(oldPtr, 100);
if(newPtr == NULL) //deal with problems
else oldPtr = newPtr;
Can't i just do this instead? :
int *oldPtr = malloc(10);
if(realloc(oldPtr, 100) == NULL) //deal with problems
//else not necessary, oldPtr has already been reallocated and has now 100 elements
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…