-
-
Save meteficha/3209359 to your computer and use it in GitHub Desktop.
Realloc copy operations
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
#include <stdio.h> | |
#include <stdlib.h> | |
const size_t MAX_SIZE = 1024*1024*1024; /* 1 GiB */ | |
int main(){ | |
void* ptr=NULL; | |
void* decoy=NULL; | |
void* last; | |
int n=0; | |
int i; | |
size_t size = 1024; | |
for (i=1; size < MAX_SIZE; i++){ | |
last = ptr; | |
decoy = realloc(decoy, size); | |
ptr = realloc(ptr, size); | |
size = size * 2; | |
if(last!=ptr) n++; | |
} | |
printf("Total op = %d\nNumber of copy operations = %d\n", i, n); | |
return(0); | |
} |
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
Total op = 21 | |
Number of copy operations = 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment