Skip to content

Commit 5a8306f

Browse files
Merge pull request hsf-training#216 from bernhardmgruber/raii
Use less C in the RAII introduction slides
2 parents 3fc20f3 + 2f0149d commit 5a8306f

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

talk/morelanguage/raii.tex

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
\begin{cppcode*}{xleftmargin=20pt}
88
char *s;
99
try {
10-
callThatThrows();
11-
s = (char*) malloc(...);
12-
strncpy(s, ...);
10+
foo(); // may throw
11+
s = new char[100];
12+
read_line(s);
1313
} catch (...) { ... }
14-
bar(s);
14+
process_line(s);
1515
\end{cppcode*}
1616
\end{exampleblock}
1717
\pause
@@ -20,11 +20,11 @@
2020
\begin{exampleblock}{They need to be released
2121
\hfill \onslide<4->{\textcolor{orange}{\bf Memory leak}}}
2222
\begin{cppcode*}{xleftmargin=20pt}
23-
char *s = (char*) malloc(...);
24-
strncpy(s, ...);
25-
if (0 != strncmp(s, ...)) return;
26-
foo(s);
27-
free(s);
23+
char *s = new char[100];
24+
read_line(s);
25+
if (s[0] == '#') return;
26+
process_line(s);
27+
delete[] s;
2828
\end{cppcode*}
2929
\end{exampleblock}
3030
\pause
@@ -33,12 +33,12 @@
3333
\begin{exampleblock}{They need clear ownership
3434
\hfill \onslide<6->{\textcolor{orange}{\bf Who should release ?}}}
3535
\begin{cppcode*}{xleftmargin=20pt}
36-
char *s = (char*) malloc(...);
37-
strncpy(s, ...);
38-
someVector.push_back(s);
39-
someSet.add(s);
40-
std::thread t1(vecConsumer, someVector);
41-
std::thread t2(setConsumer, someSet);
36+
char *s = new char[100];
37+
read_line(s);
38+
vec.push_back(s);
39+
set.add(s);
40+
std::thread t1(func1, vec);
41+
std::thread t2(func2, set);
4242
\end{cppcode*}
4343
\end{exampleblock}
4444
\end{frame}
@@ -47,16 +47,19 @@
4747
\frametitlecpp[11]{This problem exists for any resource}
4848
\begin{exampleblock}{For example with a file}
4949
\begin{cppcode*}{}
50-
try {
51-
FILE *handle = std::fopen(path, "w+");
52-
if (nullptr == handle) { throw ... }
53-
if (std::fputs(str, handle) == EOF) {
54-
throw ...
55-
}
56-
fclose(handle);
57-
} catch (...) { ... }
50+
std::FILE *handle = std::fopen(path, "w+");
51+
if (nullptr == handle) { throw ... }
52+
std::vector v(100, 42);
53+
write(handle, v);
54+
if (std::fputs("end", handle) == EOF) {
55+
return;
56+
}
57+
std::fclose(handle);
5858
\end{cppcode*}
5959
\end{exampleblock}
60+
\begin{block}{}
61+
Which problems do you spot in the above snippet?
62+
\end{block}
6063
\end{frame}
6164

6265
\begin{frame}
@@ -80,17 +83,17 @@
8083
class File {
8184
public:
8285
File(const char* filename) :
83-
m_file_handle(std::fopen(filename, "w+")) {
84-
if (m_file_handle == NULL) { throw ... }
86+
m_handle(std::fopen(filename, "w+")) {
87+
if (m_handle == nullptr) { throw ... }
8588
}
86-
~File() { std::fclose(m_file_handle); }
89+
~File() { std::fclose(m_handle); }
8790
void write (const char* str) {
88-
if (std::fputs(str, m_file_handle) == EOF) {
91+
if (std::fputs(str, m_handle) == EOF) {
8992
throw ...
9093
}
9194
}
9295
private:
93-
FILE* m_file_handle;
96+
std::FILE* m_handle;
9497
};
9598
\end{cppcode*}
9699
\end{exampleblock}
@@ -113,9 +116,7 @@
113116
\end{cppcode*}
114117
\end{exampleblock}
115118
\begin{alertblock}{}
116-
\begin{itemize}
117-
\item on real projects, use \mintinline{cpp}{std::fstream} to handle files
118-
\end{itemize}
119+
The standard library provides \mintinline{cpp}{std::fstream} to handle files, use it!
119120
\end{alertblock}
120121
\end{frame}
121122

@@ -200,7 +201,7 @@
200201
\frametitlecpp[11]{RAII or raw pointers}
201202
\begin{block}{When to use what ?}
202203
\begin{itemize}
203-
\item Always use RAII for all resources, in particular allocations
204+
\item Always use RAII for resources, in particular allocations
204205
\item You thus never have to release / deallocate yourself
205206
\item Use raw pointers as non-owning, re-bindable observers
206207
\item Remember that \mintinline{cpp}{unique_ptr} is move only

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy