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 the double free problem. I understand that it is the process of calling free()
on the same memory location twice, but I have a couple of questions that I'm not 100% sure how to answer:
Question 1: What is the result of a double free in C, and why is it such a problem?
This will cause a double free:
char* ptr = malloc(sizeof(char));
*ptr = 'a';
free(ptr);
free(ptr);
My response to this would be that it would return a 0x0 memory address and cause a system instability/crash. Also if I remember correctly, a double free can actually call malloc
twice which results in a buffer overflow thus leaving the system vulnerable.
What would be the best way to briefly sum up this question?
Question 2: Describe a situation in which it is particularly easy to introduce a double free in C?
I was thinking when passing pointers around you may accidentally free it in one function, and also free it again without realising?
Again, what is the "best" way to sum this up?
"...would it return a 0x0 memory address..."
- what is this about? Would what return a 0x0 memory address? Functionfree
does not return anything. – Canister