Repository navigation
Expand file tree
/
Copy pathsimple-string.cpp
More file actions
82 lines (70 loc) · 2.07 KB
/
Copy pathsimple-string.cpp
File metadata and controls
82 lines (70 loc) · 2.07 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
// 이 코드는 copy semantics이기 때문에 double free가 일어날 위험이 있음
#include <cstdio>
#include <cstring>
#include <stdexcept>
// resource acquisition is initialization (RAII) pattern
// OR constructor acquires, destructor releases (CADRe) pattern
struct SimpleString {
SimpleString(size_t max_size) : max_size{max_size}, length{} {
if (max_size == 0) {
throw std::runtime_error{"Max size must be at least 1."};
}
buffer = new char[max_size];
buffer[0] = 0;
}
~SimpleString() { delete[] buffer; }
void print(const char* tag) const { printf("%s: %s", tag, buffer); }
bool append_line(const char* x) {
const auto x_len = strlen(x);
if (x_len + length + 2 > max_size) return false;
std::strncpy(buffer + length, x, max_size - length);
length += x_len;
buffer[length++] = '\n';
buffer[length] = 0;
return true;
}
private:
size_t max_size;
char* buffer;
size_t length;
};
struct SimpleStringOwner {
SimpleStringOwner(const char* x) : string{10} {
if (!string.append_line(x)) {
throw std::runtime_error{"Not enough memory!"};
}
string.print("Constructed");
}
~SimpleStringOwner() { string.print("About to destroy"); }
private:
SimpleString string;
};
void fn_c() { SimpleStringOwner c{"cccccccccc"}; }
void fn_b() {
SimpleStringOwner b{"b"};
fn_c();
}
int main() {
SimpleString string{115};
string.append_line("Starbuck, whaddya hear?");
string.append_line("Nothin' but the rain.");
string.print("A");
string.append_line("Grab your gun and bring the cat in.");
string.append_line("Aye-aye sir, coming home.");
string.print("B");
if (!string.append_line("Galactica!")) {
printf("String was not big enough to append another message.\n");
}
printf("------------------------\n");
SimpleStringOwner x{"x"};
printf("x is alive\n");
printf("------------------------\n");
try {
SimpleStringOwner a{"a"};
fn_b();
SimpleStringOwner d{"d"};
} catch (std::exception& e) {
printf("Exception: %s\n", e.what());
}
printf("------------------------\n");
}