Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Result of using millions of malloc()s and free()s in your C code?

I was recently asked this question in an interview.

Suppose there is a large library of C programs and each program constantly malloc()s and free()s blocks of data. What do you think will happen if there are a million calls to malloc() and free() in one run of your program. What will you add to your answer if you have been given a very large memory heap storage?

like image 829
Batwoman05 Avatar asked Jul 24 '18 20:07

Batwoman05


1 Answers

One thing that may happen is that your memory will be fragmented, especially if you allocate block of different sizes.

Thus, if your memory size is not large, some malloc may fail, even if the total free memory is bigger that requested.

like image 200
jaudo Avatar answered Nov 08 '22 03:11

jaudo