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)
//De
the correct way to call realloc is to save the return value in a temporary variable and check it for NULL. That way if realloc has failed, you haven't lost your original memory. For example:
int *a, *b;
a = malloc(10);
b = realloc(a, 100);
if (b == NULL) {
// handle error and exit
}
a = b;
EDIT: Note that if the error handling doesn't exit, you should put the last line above, i.e. a = b; inside an else clause.