-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector-realloc.cpp
More file actions
54 lines (47 loc) · 1.44 KB
/
Copy pathvector-realloc.cpp
File metadata and controls
54 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* Copyright (C) 2012, Tino Didriksen Consult
* Developed by Tino Didriksen <tino@didriksen.cc>
*
* This file is part of Benchmarks
*
* Benchmarks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Benchmarks is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Benchmarks. If not, see <http://www.gnu.org/licenses/>.
*/
#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);
int *old_v = &v[0];
size_t cnt_v = 0;
for (size_t i=0 ; i<10000000 ; ++i) {
c[i] = i;
c = (int*)realloc(c, sizeof(*c)*(i+2));
if (old_c != c) {
++cnt_c;
}
old_c = c;
v[i] = i;
v.resize(v.size()+1);
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;
free(c);
}