Is it required to free a pointer variable before using realloc?

Viewed 5167

Is it necessary to free memory before using realloc again for a pointer variable. Which of the following is correct?

for(i = 0; i < n; i++){
   myArray = (int *)realloc(myArray, i*sizeof(int));
}

for(i = 0; i < n; i++){
   myArray = (int *)realloc(myArray, i*sizeof(int));
   free(myArray);
   myArray = NULL;
}
4 Answers
Related