Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cpp-memory-esc17
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Francesco Giacomini
cpp-memory-esc17
Commits
37f9c933
Commit
37f9c933
authored
7 years ago
by
Francesco Giacomini
Browse files
Options
Downloads
Patches
Plain Diff
frames.tex
parent
cdbbf71e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
frames.tex
+512
-267
512 additions, 267 deletions
frames.tex
with
512 additions
and
267 deletions
frames.tex
+
512
−
267
View file @
37f9c933
%
\includeonlyframes{current}
\includeonlyframes
{
current
}
\begin{frame}
\begin{frame}
\tikz
[remember picture,overlay]
\tikz
[remember picture,overlay]
...
@@ -286,7 +286,7 @@ bool less(int n, int m) \{ return n < m; \}\end{codeblock}
...
@@ -286,7 +286,7 @@ bool less(int n, int m) \{ return n < m; \}\end{codeblock}
\begin{codeblock}
\begin{codeblock}
\uncover
<1->
{
std::array<int,3> a =
\{
123, 456, 789
\}
;
}
\uncover
<1->
{
std::array<int,3> a =
\{
123, 456, 789
\}
;
}
\uncover
<2->
{
\alert
<2>
{
auto first = a.begin();
}
// or std::begin(a)
}
\uncover
<2->
{
\alert
<2>
{
auto first = a.begin();
}
// or std::begin(a)
}
\uncover
<3->
{
\alert
<3>
{
auto
\
alt
<14>
{
\alert
{
const
}}
{}
last = a.end();
}
\
alt
<14>
{}
{
}
// or std::end(a)
}
\uncover
<3->
{
\alert
<3>
{
auto
\
only
<14
-
>
{
\alert
{
const
}}
last = a.end();
}
\
only
<-13>
{
}
// or std::end(a)
}
\uncover
<4->
{
while (
\alert
<4,7,10,13>
{
first != last
}
)
\{
\uncover
<4->
{
while (
\alert
<4,7,10,13>
{
first != last
}
)
\{
...
\alert
<5,8,11>
{
*first
}
...;
...
\alert
<5,8,11>
{
*first
}
...;
\alert
<6,9,12>
{
++first
}
;
\alert
<6,9,12>
{
++first
}
;
...
@@ -380,7 +380,7 @@ find(Iterator first, Iterator last, const T\& value)
...
@@ -380,7 +380,7 @@ find(Iterator first, Iterator last, const T\& value)
\begin{itemize}
\begin{itemize}
\item
Examples
\item
Examples
\begin{codeblock}
\begin{codeblock}
std::vector<int> v =
{
2, 5, 4, 0, 1
}
;
std::vector<int> v =
\
{
2, 5, 4, 0, 1
\
}
;
// sort the first half of the vector in ascending order
// sort the first half of the vector in ascending order
std::sort(std::begin(v), std::begin(v) + v.size() / 2);
std::sort(std::begin(v), std::begin(v) + v.size() / 2);
...
@@ -595,8 +595,8 @@ accumulate(\ldots,
...
@@ -595,8 +595,8 @@ accumulate(\ldots,
\uncover
<2->
{
class SomeUniqueName
\{
\uncover
<2->
{
class SomeUniqueName
\{
\uncover
<5->
{
int v
\_
;
}
\uncover
<5->
{
int v
\_
;
}
public:
public:
explicit SomeUniqueName(
\uncover
<5->
{
int v
}
)
\uncover
<5->
{
explicit SomeUniqueName(int v)
\uncover
<5->
{
: v
\_\{
v
\}
}
\{\}
: v
\_\{
v
\}
\{\}
}
\alt
<-7>
{
auto
}{
\alert
<8>
{
int
}}
operator()
\uncover
<3->
{
(int i)
\alt
<-7>
{
auto
}{
\alert
<8>
{
int
}}
operator()
\uncover
<3->
{
(int i)
\{
return i + v
\uncover
<5->
{
\_
}
;
\}
}
\{
return i + v
\uncover
<5->
{
\_
}
;
\}
}
\}
;
}
\}
;
}
...
@@ -1171,6 +1171,513 @@ auto s = std::shared\_ptr<FILE>\{
...
@@ -1171,6 +1171,513 @@ auto s = std::shared\_ptr<FILE>\{
\end{columns}
\end{columns}
\end{frame}
\end{frame}
\begin{frame}
\begin{itemize}
\item
Try the code snippets
\item
C++ and Memory
$
\rightarrow
$
Pointers
\item
Adapt the previous exercises to use smart pointers
\item
Starting from
\code
{
dir.cpp
}
, write code to:
\begin{itemize}
\item
create a smart pointer managing a
\code
{
DIR
}
resource obtained with
the
\code
{
opendir
}
function call
\item
associate a deleter to that smart pointer
\item
implement a function to read the names of the files in that directory
\item
check if the deleter is called at the right moment
\item
hide the creation of the smart pointer behind a factory function
\item
populate a vector of FILEs, properly wrapped in a smart pointer,
obtained opening the regular files in that directory
\item
\ldots
\end{itemize}
\end{itemize}
\end{frame}
\section
{
Move semantics
}
\begin{frame}
[fragile,label=current]
{
Copy vs move
}
\begin{columns}
\begin{column}
{
.45
\textwidth
}
\begin{codeblock}
{
\tiny
class String
\{
char* s
\_
;
\ldots
\}
;
}
\end{codeblock}
\begin{codeblock}
<1->
{
\tiny
String s1
\{
"Hi!"
\}
;
\uncover
<2->
{
String s2
\{
s1
\}
;
}}
\end{codeblock}
\uncover
<3->
{
\small
\begin{itemize}
\item
Both
\code
{
s1
}
and
\code
{
s2
}
exist at the end
\item
The ``deep'' copy is needed
\end{itemize}
}
\begin{codeblock}
<4->
{
\tiny
String get
\_
string()
\{
return "Hey";
\}
String
\alert
<6>
{
s3
}
\{\alert
<5>
{
get
\_
string()
}
\}\alert
<7>
{
;
}}
\end{codeblock}
\uncover
<8->
{
\small
\begin{itemize}
\item
Only
\code
{
s3
}
exists at the end
\item
The ``deep'' copy is a waste
\end{itemize}
}
\end{column}
\begin{column}
{
.55
\textwidth
}
\newcommand*
{
\stackx
}{
0.
}
\newcommand*
{
\heapx
}{
3.0
}
\begin{tikzpicture}
[anchor=south west]
\node
at (
\stackx
,0) [
rectangle,
minimum width=2cm,
minimum height=7cm,
draw=black,
label=
{
90:
{
\scriptsize
stack
}}
]
{}
;
\node
at (
\heapx
,0) [
rectangle,
minimum width=2cm,
minimum height=7cm,
draw=black,
label=
{
90:
{
\scriptsize
heap
}}
]
{}
;
\node
at (
\heapx
-0.1,4.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hi!
\char
`
\\
0
}
;
\node
at (
\heapx
,5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4abc
}}
]
{}
;
\node
at (
\stackx
-0.1,4.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
s1
}}
,
]
{}
;
\node
at (
\stackx
,5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4abc
}
;
\visible
<2->
{
\node
at (
\heapx
-0.1,3.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,4) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hi!
\char
`
\\
0
}
;
\node
at (
\heapx
,4) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4ab0
}}
] (r2)
{}
;
\node
at (
\stackx
-0.1,3.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
s2
}}
,
]
{}
;
\node
at (
\stackx
,4) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
] (h2)
{
\scriptsize\tt
0x4ab0
}
;
%\draw[arrow] (h2) -- (r2);
}
\visible
<6->
{
\node
at (
\heapx
-0.1,2.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,2.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hey
\char
`
\\
0
}
;
\node
at (
\heapx
,2.5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4aac
}}
]
{}
;
\node
at (
\stackx
-0.1,2.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
s3
}}
,
]
{}
;
\node
at (
\stackx
,2.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4aac
}
;
}
\visible
<5-6>
{
\node
at (
\heapx
-0.1,1.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,1.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hey
\char
`
\\
0
}
;
\node
at (
\heapx
,1.5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4aa0
}}
]
{}
;
\node
at (
\stackx
-0.1,1.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
]
{}
;
\node
at (
\stackx
,1.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4aa0
}
;
}
\end{tikzpicture}
\end{column}
\end{columns}
\end{frame}
\begin{frame}
[fragile,label=current]
{
Copy vs move
\insertcontinuationtext
}
\begin{columns}
\begin{column}
{
.45
\textwidth
}
\begin{codeblock}
{
\tiny
\uncover
<1->
{
class String
\{
char* s
\_
;
public:
String(char const* s)
\{
size
\_
t size = strlen(s) + 1;
s
\_
= new char[size];
memcpy(s
\_
, s, size);
\}
~String()
\{
delete [] s
\_
;
\}
}
\uncover
<2->
{
//
\alert
<2>
{
copy
}
String(String const
&
other)
\{
size
\_
t size = strlen(other.s
\_
) + 1;
s
\_
= new char[size];
memcpy(s
\_
, other.s
\_
, size);
\}
}
\uncover
<4->
{
//
\alert
<4-5>
{
move
}
String(
\alert
<7>
{
???
}
tmp): s
\_
(tmp.s
\_
)
\{
\uncover
<5->
{
tmp.s
\_
= nullptr;
}
\}
}
\uncover
<1->
{
\ldots
\}
;
String s1
\{
"Hi!"
\}
;
}
\uncover
<2->
{
String s2
\{
s1
\}
;
}
\uncover
<3->
{
String
\alert
<4>
{
s3
}
\{\alert
<3>
{
get
\_
string()
}
\}\alert
<6>
{
;
}}
}
\end{codeblock}
\end{column}
\begin{column}
{
.55
\textwidth
}
\newcommand*
{
\stackx
}{
0.
}
\newcommand*
{
\heapx
}{
3.0
}
\begin{tikzpicture}
[anchor=south west]
\node
at (
\stackx
,0) [
rectangle,
minimum width=2cm,
minimum height=7cm,
draw=black,
label=
{
90:
{
\scriptsize
stack
}}
]
{}
;
\node
at (
\heapx
,0) [
rectangle,
minimum width=2cm,
minimum height=7cm,
draw=black,
label=
{
90:
{
\scriptsize
heap
}}
]
{}
;
\node
at (
\heapx
-0.1,4.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hi!
\char
`
\\
0
}
;
\node
at (
\heapx
,5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4abc
}}
]
{}
;
\node
at (
\stackx
-0.1,4.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
s1
}}
,
]
{}
;
\node
at (
\stackx
,5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4abc
}
;
\visible
<2->
{
\node
at (
\heapx
-0.1,3.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,4) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hi!
\char
`
\\
0
}
;
\node
at (
\heapx
,4) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4ab0
}}
] (r2)
{}
;
\node
at (
\stackx
-0.1,3.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
s2
}}
,
]
{}
;
\node
at (
\stackx
,4) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
] (h2)
{
\scriptsize\tt
0x4ab0
}
;
%\draw[arrow] (h2) -- (r2);
}
\visible
<4->
{
\node
at (
\stackx
-0.1,2.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
s3
}}
,
]
{}
;
\node
at (
\stackx
,2.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
\alert
<4>
{
0x4aa0
}}
;
}
\visible
<3->
{
\node
at (
\heapx
-0.1,1.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,1.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
Hey
\char
`
\\
0
}
;
\node
at (
\heapx
,1.5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4aa0
}}
]
{}
;
}
\visible
<3-5>
{
\node
at (
\stackx
-0.1,1.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
]
{}
;
\node
at (
\stackx
,1.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
\alt
<3-4>
{
\alert
<4>
{
0x4aa0
}}{
\code
{
\alert
<5>
{
nullptr
}}}}
;
}
\end{tikzpicture}
\end{column}
\end{columns}
\end{frame}
\begin{frame}
[label=current]
{
lvalues vs rvalues
}
\begin{itemize}
\item
The taxonomy of values in C++ is complex
\begin{itemize}
\item
glvalue, prvalue, xvalue, lvalue, rvalue
\end{itemize}
\item
We can assume
\begin{description}
\item
[lvalue] A named object
\begin{itemize}
\item
for which you can take the address
\item
\alert
{
l
}
stands for ``left'' because it used to represent the
\alert
{
l
}
eft-hand side of an assignment
\end{itemize}
\item
[rvalue] An unnamed (temporary) object
\begin{itemize}
\item
for which you can't take the address
\item
\alert
{
r
}
stands for ``right'' because it used to represent the
\alert
{
r
}
ight-hand side of an assignment
\end{itemize}
\end{description}
\end{itemize}
\end{frame}
\begin{frame}
[fragile,label=current]
{
Rvalue reference
}
\begin{itemize}
\item
A
\texttt
{
T
\&\&
}
is an rvalue reference
\begin{itemize}
\item
introduced in C++11
\end{itemize}
\item
It binds to rvalues but not to lvalues
\end{itemize}
\begin{codeblock}
<2->
{
\tiny
class Thing;
Thing make
\_
thing();
Thing t;
\uncover
<3->
{
Thing
&
r = t;
}
\uncover
<4->
{
// ok
}
\uncover
<5->
{
Thing
&&
r = t;
}
\uncover
<6->
{
// error
}
\uncover
<7->
{
Thing
&
r = make
\_
thing();
}
\uncover
<8->
{
// error
}
\uncover
<9->
{
Thing
&&
r = make
\_
thing();
}
\uncover
<10->
{
// ok
}
\uncover
<11->
{
Thing const
&
r = make
\_
thing();
}
\uncover
<12->
{
// ok (!)
}
\uncover
<13->
{
Thing const
&&
r = make
\_
thing();
}
\uncover
<14->
{
// ok, but what for?
}}
\end{codeblock}
\begin{codeblock}
<15->
{
\tiny
class String
\{
// move constructor
String(String
\alert
<15>
{&&}
tmp) : s
\_
(tmp.s
\_
)
\{
tmp.s
\_
= nullptr;
\}
\}
;
String s2
\{
s1
\}
; // call String::String(String const
&
)
String s3
\{
get
\_
string()
\}
; // call String::String(String
&&
)
}
\end{codeblock}
\end{frame}
\begin{frame}
[fragile,label=current]
{
Rvalue reference
\insertcontinuationtext
}
\begin{itemize}
\item
Any function can accept rvalue references
\begin{codeblock}
void foo(String
&&
);
foo(get
\_
string());
foo(String
\{
"hello"
\}
);
\end{codeblock}
\item
lvalues can be explicitly transformed into rvalues
\begin{codeblock}
String s;
foo(s); // error
foo(std::move(s)); // ok, I don't care any more about s
s.size(); // dangerous
\end{codeblock}
\end{itemize}
\end{frame}
\begin{frame}
[fragile]
{
Smart pointers
\insertcontinuationtext
}
\begin{frame}
[fragile]
{
Smart pointers
\insertcontinuationtext
}
\begin{columns}
[c]
\begin{columns}
[c]
...
@@ -2099,241 +2606,6 @@ Handle h1\{10\}, h2\{20\};
...
@@ -2099,241 +2606,6 @@ Handle h1\{10\}, h2\{20\};
arrow/.style=
{
->
}
arrow/.style=
{
->
}
}
}
\begin{frame}
[fragile]
{
Copy vs move
}
\begin{columns}
\begin{column}
{
.45
\textwidth
}
\begin{codeblock}
<1->
Handle h1
\{
123
\}
;
\uncover
<2->
{
Handle h2
\{
h1
\}
;
}
\end{codeblock}
\uncover
<3->
{
\small
\begin{itemize}
\item
Both
\texttt
{
h1
}
and
\texttt
{
h2
}
exist at the end
\item
The ``deep'' copy is needed
\end{itemize}
}
\begin{codeblock}
<4->
Handle make
\_
holder();
Handle
\alert
<6>
{
h3
}
\{\alert
<5>
{
make
\_
holder()
}
\}\alert
<7>
{
;
}
\end{codeblock}
\uncover
<8->
{
\small
\begin{itemize}
\item
Only
\texttt
{
h3
}
exists at the end
\item
The ``deep'' copy is a waste
\item
Could reuse the ``guts'' of the temporary
\end{itemize}
}
\end{column}
\begin{column}
{
.55
\textwidth
}
\newcommand*
{
\stackx
}{
0.
}
\newcommand*
{
\heapx
}{
3.0
}
\begin{tikzpicture}
[anchor=south west]
\node
at (
\stackx
,0) [
rectangle,
minimum width=2cm,
minimum height=7cm,
draw=black,
label=
{
90:
{
\scriptsize
stack
}}
]
{}
;
\node
at (
\heapx
,0) [
rectangle,
minimum width=2cm,
minimum height=7cm,
draw=black,
label=
{
90:
{
\scriptsize
heap
}}
]
{}
;
\node
at (
\heapx
-0.1,4.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
123
}
;
\node
at (
\heapx
,5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4abc
}}
]
{}
;
\node
at (
\stackx
-0.1,4.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
h1
}}
,
]
{}
;
\node
at (
\stackx
,5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4abc
}
;
\visible
<2->
{
\node
at (
\heapx
-0.1,3.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,4) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
123
}
;
\node
at (
\heapx
,4) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4ab0
}}
] (r2)
{}
;
\node
at (
\stackx
-0.1,3.9) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
h2
}}
,
]
{}
;
\node
at (
\stackx
,4) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
] (h2)
{
\scriptsize\tt
0x4ab0
}
;
%\draw[arrow] (h2) -- (r2);
}
\visible
<6->
{
\node
at (
\heapx
-0.1,2.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,2.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
456
}
;
\node
at (
\heapx
,2.5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4aac
}}
]
{}
;
\node
at (
\stackx
-0.1,2.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
label=
{
180:
{
\scriptsize\tt
h3
}}
,
]
{}
;
\node
at (
\stackx
,2.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4aac
}
;
}
\visible
<5-6>
{
\node
at (
\heapx
-0.1,1.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=0.7cm,
]
{}
;
\node
at (
\heapx
,1.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white
]
{
\scriptsize\tt
456
}
;
\node
at (
\heapx
,1.5) [
rectangle,
minimum width=.5cm,
minimum height=.5cm,
draw=black,
densely dotted,
label=
{
270:
{
\scriptsize\tt
0x4aa0
}}
]
{}
;
\node
at (
\stackx
-0.1,1.4) [
rectangle,
fill=green!10!white,
draw=black!40,
minimum width=2.2cm,
minimum height=.7cm,
]
{}
;
\node
at (
\stackx
,1.5) [
rectangle,
minimum width=2cm,
minimum height=.5cm,
draw=black,
fill=green!30!white,
]
{
\scriptsize\tt
0x4aa0
}
;
}
\end{tikzpicture}
\end{column}
\end{columns}
\end{frame}
\section
{
Move semantics
}
\begin{frame}
{
lvalues vs rvalues
}
\begin{itemize}
\item
The taxonomy of values in C++ is complex
\begin{itemize}
\item
glvalue, prvalue, xvalue, lvalue, rvalue
\end{itemize}
\item
We can assume
\begin{description}
\item
[lvalue] A named object
\begin{itemize}
\item
for which you can take the address
\item
\alert
{
l
}
stands for ``left'' because it used to represent the
\alert
{
l
}
eft-hand side of an assignment
\end{itemize}
\item
[rvalue] An unnamed (temporary) object
\begin{itemize}
\item
for which you can't take the address
\item
\alert
{
r
}
stands for ``right'' because it used to represent the
\alert
{
r
}
ight-hand side of an assignment
\end{itemize}
\end{description}
\end{itemize}
\end{frame}
\begin{frame}
[fragile]
{
Move constructor
}
\begin{frame}
[fragile]
{
Move constructor
}
\begin{columns}
\begin{columns}
...
@@ -2447,33 +2719,6 @@ Handle \alert<3-5>{h\{}\alert<2>{make\_holder()}\alert<3-5>{\}}\alert<6>{;}\end{
...
@@ -2447,33 +2719,6 @@ Handle \alert<3-5>{h\{}\alert<2>{make\_holder()}\alert<3-5>{\}}\alert<6>{;}\end{
\end{frame}
\end{frame}
\begin{frame}
[fragile]
{
Rvalue reference
}
\begin{itemize}
\item
A
\texttt
{
T
\&\&
}
is an rvalue reference
\item
It binds to rvalues but not to lvalues
\end{itemize}
\begin{codeblock}
<2->
{
class Thing;
Thing make
\_
thing();
Thing t;
\uncover
<3->
{
Thing
&
r = t;
}
\uncover
<4->
{
// ok
}
\uncover
<5->
{
Thing
&&
r = t;
}
\uncover
<6->
{
// error
}
\uncover
<7->
{
Thing
&
r = make
\_
thing();
}
\uncover
<8->
{
// error
}
\uncover
<9->
{
Thing
&&
r = make
\_
thing();
}
\uncover
<10->
{
// ok
}
\uncover
<11->
{
Thing const
&
r = make
\_
thing();
}
\uncover
<12->
{
// ok (!)
}
\uncover
<13->
{
Thing const
&&
r = make
\_
thing();
}
\uncover
<14->
{
// ok, but what for?
}}
\end{codeblock}
\uncover
<15->
{
\begin{itemize}
\item
A reference extends the lifetime of a temporary object until the end of the scope
\end{itemize}
}
\end{frame}
\begin{frame}
[fragile]
{
Move assignment
}
\begin{frame}
[fragile]
{
Move assignment
}
\begin{columns}
\begin{columns}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment