I have two questions.
- How does the
free
function in C work? - How come the pointer variable updates itself to store the new address?
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int *p;
p = (int *)malloc(sizeof(int));
*p = 10;
int *q = p;
printf("p:%d\n", p);
printf("q:%d\n", q);
printf("*p:%d\n", *p);
printf("*q:%d\n", *q);
free(p);
printf("Memory freed.\n");
p = (int *)malloc(sizeof(int));
*p = 19;
printf("p:%d\n", p);
printf("q:%d\n", q);
printf("*p:%d\n", *p);
printf("*q:%d\n", *q);
}
How come the output is like this?
p:2067804800
q:2067804800
*p:10
*q:10
Memory freed.
p:2067804800
q:2067804800
*p:19
*q:19