Last active
November 18, 2020 12:39
-
-
Save zer1t0/279c1b585960c7af84746fac196eabb6 to your computer and use it in GitHub Desktop.
Get chunk size for a malloc allocation
This file contains 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
/* | |
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