Skip to content

Instantly share code, notes, and snippets.

@sonney2k
Created July 21, 2013 21:48
Show Gist options
  • Save sonney2k/6050125 to your computer and use it in GitHub Desktop.
Save sonney2k/6050125 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string.h>
int main() {
int *c = (int*)malloc(sizeof(*c));
int *old_c = c;
size_t cnt_c = 0;
std::vector<int> v(1);
int *old_v = &v[0];
size_t cnt_v = 0;
for (size_t i=0 ; i<1000000 ; ++i) {
c[i] = i;
c = (int*)realloc(c, sizeof(*c)*(100*i+2));
if (old_c != c) {
++cnt_c;
}
old_c = c;
}
//for (size_t i=0 ; i<1000000*100 ; ++i) {
// c[i]=0;
//}
//memset(c,0,sizeof(*c)*100*1000000);
std::cout << "realloc() reallocations: " << cnt_c << std::endl;
std::cout << "std::vector reallocations: " << cnt_v << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment