Skip to content

Instantly share code, notes, and snippets.

@zer1t0
Last active November 18, 2020 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zer1t0/279c1b585960c7af84746fac196eabb6 to your computer and use it in GitHub Desktop.
Save zer1t0/279c1b585960c7af84746fac196eabb6 to your computer and use it in GitHub Desktop.
Get chunk size for a malloc allocation
/*
Get chunk size for a malloc allocation
*/
#include <stdio.h>
#include <stdlib.h>
#define NO_FLAGS ~0x7
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s <malloc-size>\n", argv[0]);
return 1;
}
int malloc_size = atoi(argv[1]);
void* chunk = malloc(malloc_size);
// access to size member of chunk
size_t chunk_size = *((size_t*)chunk - 1);
printf("chunk size: 0x%lx\n", chunk_size & NO_FLAGS);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment