Skip to content

Instantly share code, notes, and snippets.

@gsingh93
Created September 23, 2016 06:59
Show Gist options
  • Save gsingh93/40e23880c717b5ffc1c63ea713a22cd9 to your computer and use it in GitHub Desktop.
Save gsingh93/40e23880c717b5ffc1c63ea713a22cd9 to your computer and use it in GitHub Desktop.
Print the requested, usable, and real sizes of a heap chunk
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main() {
printf("size of size_t: %zu\n", sizeof(size_t));
int i;
for (i = 0; i < 10; i++) {
size_t size = i * 8;
void *m = malloc(size);
printf("requested chunk of size %zu\n", size);
printf("malloc_usable_size: %zu\n", malloc_usable_size(m));
printf("real_size: %zu\n", *((size_t*) (m - sizeof(size_t))) - 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment