C++

Asked • 08/07/19

How do malloc() and free() work?

I want to know how `malloc` and `free` work. int main() {  unsigned char *p = (unsigned char*)malloc(4*sizeof(unsigned char));  memset(p,0,4);  strcpy((char*)p,"abcdabcd"); // **deliberately storing 8bytes**  cout << p;  free(p); // Obvious Crash, but I need how it works and why crash.  cout << p; return 0; } I would be really grateful if the answer is in depth at memory level, if it's possible.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.