Last active
May 20, 2020 13:45
-
-
Save sw17ch/7100909 to your computer and use it in GitHub Desktop.
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 <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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you both (again)! At some point, I owe you both a beverage of choice.