I'm studying about the mechanism of malloc() and free() function in C. After few test, I discern that the mechanism of those function follows worst fit algorithm, doesn't it.
#include <stdio.h>
#include <stdlib.h>
int main()
{
void* base;
void* a = malloc(100);
base = a;
void* b = malloc(60);
printf("%ld %ld\n", a-base, b-base);
free(a);
a = malloc(40);
printf("Free then reallocate a\n%ld %ld\n", a-base, b-base);
void* c= malloc(1);
printf("Allocate c c\n%ld %ld %ld\n", a-base, b-base, c-base);
free(b);
b = malloc(1);
printf("Free then reallocate b\n%ld %ld %ld\n", a-base, b-base, c-base);
return 0;
}
Output:
0 112
Free then reallocate a
720 112
Allocate c c
720 112 768
Free then reallocate b
720 800 768