As the title suggests I am new to C and have a mid-term coming up shortly. I am revising from past papers currently and a recurring theme is double free problem. I understan
This question has been well answered, but I add a late answer due to a "duplicate question" link, which asked "how to avoid it?"
One line is added to the example code posted.
char* ptr = malloc(sizeof(char));
*ptr = 'a';
free(ptr);
ptr = NULL; // add this
free(ptr);
Function free does nothing with a NULL pointer.