Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
148 lines (92 loc) · 3.12 KB
/
Copy pathmain.cpp
File metadata and controls
148 lines (92 loc) · 3.12 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* /////////////////////////////////////////////////////////////////////////
* File: by_library/memory/auto_buffer/auto_buffer.cpp
*
* Purpose: C++ example program demonstrating use of the Memory library
* component auto_buffer.
*
* Created: 15th September 2006
* Updated: 30th May 2025
*
* ////////////////////////////////////////////////////////////////////// */
/* STLSoft header files */
#include <stlsoft/memory/auto_buffer.hpp>
/* Standard C++ header files */
#include <exception>
#include <iostream>
#include <vector>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
/* Standard C header files */
#include <stdlib.h> // for EXIT_FAILURE, EXIT_SUCCESS
/* /////////////////////////////////////////////////////////////////////////
* types
*/
typedef stlsoft::auto_buffer<char, 128> buffer_t;
// typedef std::vector<char> buffer_t;
/* /////////////////////////////////////////////////////////////////////////
* helper functions
*/
void
do_a_gazillion_allocations()
{
for (size_t i = 0; i != 100; ++i)
{
for (size_t j = 0; j != 1000; ++j)
{
buffer_t buffer(1 + i * 10 + j * 1000);
}
}
}
/* /////////////////////////////////////////////////////////////////////////
* main()
*/
int main()
{
try
{
buffer_t buff(0);
// Resize within the bounds of the internal buffer
buff.resize(10, 'a');
char* const p0 = buff.data();
::memset(&buff[0], '1', buff.size());
// Resize outside the bounds of the internal buffer, and go to the heap
buff.resize(100, 'b');
char* const p1 = buff.data();
::memset(&buff[0], '2', buff.size());
do_a_gazillion_allocations();
// Resize again outside the bounds of the internal buffer, and go to the heap
buff.resize(10000, 'c');
char* const p2 = buff.data();
::memset(&buff[0], '3', buff.size());
do_a_gazillion_allocations();
// Resize outside the bounds of the internal buffer, and go to the heap
buff.resize(100, 'd');
char* const p3 = buff.data();
::memset(&buff[0], '4', buff.size());
do_a_gazillion_allocations();
// Resize again outside the bounds of the internal buffer but within the previous external allocation, which will not require another trip to the heap
buff.resize(10000, 'e');
char* const p4 = buff.data();
::memset(&buff[0], '5', buff.size());
do_a_gazillion_allocations();
// Resize again outside the bounds of the internal buffer, and go to the heap
buff.resize(10001, 'f');
char* const p5 = buff.data();
::memset(&buff[0], '6', buff.size());
do_a_gazillion_allocations();
return EXIT_SUCCESS;
}
catch (std::exception& x)
{
cerr << "Error: " << x.what() << endl;
return EXIT_FAILURE;
}
catch (...)
{
cerr << "Unknown error" << endl;
return EXIT_FAILURE;
}
}
/* ///////////////////////////// end of file //////////////////////////// */