Skip to content

Instantly share code, notes, and snippets.

@meteficha
Forked from danielsaad/Source Code
Created July 30, 2012 19:25
Show Gist options
  • Save meteficha/3209359 to your computer and use it in GitHub Desktop.
Save meteficha/3209359 to your computer and use it in GitHub Desktop.
Realloc copy operations
#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);
}
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