Skip to content

Instantly share code, notes, and snippets.

@sonney2k
Created July 21, 2013 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonney2k/6050121 to your computer and use it in GitHub Desktop.
Save sonney2k/6050121 to your computer and use it in GitHub Desktop.
#include <cstdlib>
#include <iostream>
#include <vector>
int main() {
int *c = (int*)malloc(sizeof(*c));
int *old_c = c;
size_t cnt_c = 0;
std::vector<int> v(1);
//v.reserve(100000000);
int *old_v = &v[0];
size_t cnt_v = 0;
for (size_t i=0 ; i<1000000 ; ++i) {
v[i] = i;
v.resize(100*i+2);
if (old_v != &v[0]) {
++cnt_v;
}
old_v = &v[0];
}
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