I want to know how malloc and free work.
int main() {
unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char));
m
This has nothing specifically to do with malloc and free. Your program exhibits undefined behaviour after you copy the string - it could crash at that point or at any point afterwards. This would be true even if you never used malloc and free, and allocated the char array on the stack or statically.