Created
January 22, 2015 11:14
-
-
Save nmmmnu/fa23248571e3e498efef to your computer and use it in GitHub Desktop.
malloc calculator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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