Skip to content

Commit

Permalink
demo of calloc and realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jackr276 committed Sep 10, 2024
1 parent 0705a3b commit c087e5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Binary file modified out/test
Binary file not shown.
29 changes: 29 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ int main(){
printf("\tstruct->d = 4e10 == struct->d = %e\n", structs[r]->d);

printf("Freeing all sample structs.\n");

/**
* Using mempool_free in a loop
*/
Expand All @@ -81,6 +82,34 @@ int main(){
mempool_free(structs[i]);
}


printf("Demonstrating mempool_calloc() and coalescing of blocks.\n");
/**
* To demonstrate mempool_calloc and the nature of coalescing blocks all at once, I will call mempool_calloc
* and reserve a size that is larger than the default block size. As a reminder, this is allowed and supported,
* but if you find yourself doing this a lot, you're not using mempool correctly
*/
int* int_arr = (int*)mempool_calloc(40, sizeof(int));

printf("After mempool_calloc() and initializing:\n");
//Fill it up for the sake of demonstration
for(int i = 0; i < 40; i++){
*(int_arr + i) = i;
printf("int_arr[%d]: %d\n", i, int_arr[i]);
}

/**
* To demonstrate mempool_realloc(), we will realloc() this block to have 10 additional ints in it. Mempool_realloc()
* works the same way externally as realloc
*/
int_arr = (int*)mempool_realloc(int_arr, 50 * sizeof(int));

//Add the other ints in
for(int i = 40; i < 50; i++){
*(int_arr + i) = i;
}


printf("Destroying the mempool\n");
/**
* Teardown
Expand Down

0 comments on commit c087e5c

Please sign in to comment.