Skip to content

Instantly share code, notes, and snippets.

@danielsaad
Created July 30, 2012 19:19
Show Gist options
  • Save danielsaad/3209326 to your computer and use it in GitHub Desktop.
Save danielsaad/3209326 to your computer and use it in GitHub Desktop.
Realloc copy operations
Total op = 10000
Number of copy operations = 12
Total op = 1000000
Number of copy operations = 5941
#include <stdio.h>
#include <stdlib.h>
const int REALLOC_SIZE = 10000;
const int IT = 1000000;
int main(){
void* ptr=NULL;
void* last;
int n=0;
int i;
for(i=1;i<IT;i++){
last = ptr;
ptr = realloc(ptr,REALLOC_SIZE*i);
if(last!=ptr) n++;
}
printf("Total op = %d\nNumber of copy operations = %d\n",IT,n);
return(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment