I want to know how malloc
and free
work.
int main() {
unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char));
m
Your program crashes because it used memory that does not belong to you. It may be used by someone else or not - if you are lucky you crash, if not the problem may stay hidden for a long time and come back and bite you later.
As far as malloc/free implementation goes - entire books are devoted to the topic. Basically the allocator would get bigger chunks of memory from the OS and manage them for you. Some of the problems an allocator must address are: