Skip to content

Instantly share code, notes, and snippets.

@sw17ch
Last active May 20, 2020 13:45
Show Gist options
  • Select an option

  • Save sw17ch/7100909 to your computer and use it in GitHub Desktop.

Select an option

Save sw17ch/7100909 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define IGNORE(X) ((void)X)
#define MAX_ALLOC (1024 * 1024)
#define MIN_ALLOC (128)
int main(int argc, char * argv[]) {
IGNORE(argc);
IGNORE(argv);
void * ptr = malloc(MIN_ALLOC);
for (int i = 0; i < 100; i++) {
size_t realloc_sz = arc4random_uniform(MAX_ALLOC);
void * new_ptr = realloc(ptr, realloc_sz);
if (NULL == new_ptr) {
printf("Realloc of size: %lu. Realloc failed!\n", realloc_sz);
} else if (new_ptr != ptr) {
printf("Realloc of size: %lu. Pointer changed: (%p -> %p)\n", realloc_sz, ptr, new_ptr);
ptr = new_ptr;
} else {
printf("Realloc of size: %lu. Pointer is the same: (%p)\n", realloc_sz, ptr);
}
}
return 0;
}
@telemachus

Copy link
Copy Markdown

Thank you both (again)! At some point, I owe you both a beverage of choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment