Skip to content

Instantly share code, notes, and snippets.

@nmmmnu
Created January 22, 2015 11:14
Show Gist options
  • Save nmmmnu/fa23248571e3e498efef to your computer and use it in GitHub Desktop.
Save nmmmnu/fa23248571e3e498efef to your computer and use it in GitHub Desktop.
malloc calculator
#include <stdlib.h>
#include <stdio.h>
size_t malloc_calc(size_t size){
const size_t realloc_size = sizeof(void *) * 2;
const size_t realloc_add = sizeof(void *);
size_t newsize = size / realloc_size;
if (size % realloc_size)
newsize++;
return newsize * realloc_size + realloc_add;
}
int main(){
//void *p1 = malloc(1);
//void *p2 = malloc(16);
printf("%zu\n", sizeof(int));
printf("%zu\n", sizeof(void *));
printf("%zu %zu %zu\n", malloc_calc(1), malloc_calc(16), malloc_calc(1) + malloc_calc(16));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment