Memory Safety

1 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€59.89
List Price:  €85.57
You save:  €25.67
£52.68
List Price:  £75.27
You save:  £22.58
CA$96.24
List Price:  CA$137.50
You save:  CA$41.25
A$107.05
List Price:  A$152.93
You save:  A$45.88
S$89.86
List Price:  S$128.38
You save:  S$38.52
HK$545.76
List Price:  HK$779.69
You save:  HK$233.93
CHF 56.96
List Price:  CHF 81.37
You save:  CHF 24.41
NOK kr704.01
List Price:  NOK kr1,005.77
You save:  NOK kr301.76
DKK kr447.07
List Price:  DKK kr638.70
You save:  DKK kr191.63
NZ$118.79
List Price:  NZ$169.71
You save:  NZ$50.92
د.إ257.03
List Price:  د.إ367.21
You save:  د.إ110.17
৳8,497.01
List Price:  ৳12,139.10
You save:  ৳3,642.09
₹6,170.83
List Price:  ₹8,815.86
You save:  ₹2,645.02
RM295.70
List Price:  RM422.45
You save:  RM126.75
₦107,222.90
List Price:  ₦153,182.14
You save:  ₦45,959.23
₨19,823.65
List Price:  ₨28,320.71
You save:  ₨8,497.06
฿2,264.59
List Price:  ฿3,235.27
You save:  ฿970.67
₺2,876.58
List Price:  ₺4,109.58
You save:  ₺1,233
B$380.14
List Price:  B$543.08
You save:  B$162.94
R1,235.53
List Price:  R1,765.13
You save:  R529.59
Лв117.04
List Price:  Лв167.21
You save:  Лв50.16
₩97,214.01
List Price:  ₩138,883.11
You save:  ₩41,669.10
₪233.25
List Price:  ₪333.23
You save:  ₪99.98
₱3,995.58
List Price:  ₱5,708.22
You save:  ₱1,712.64
¥10,290.97
List Price:  ¥14,702.02
You save:  ¥4,411.05
MX$1,305.67
List Price:  MX$1,865.32
You save:  MX$559.65
QR254.68
List Price:  QR363.84
You save:  QR109.16
P940.72
List Price:  P1,343.95
You save:  P403.22
KSh9,027.58
List Price:  KSh12,897.10
You save:  KSh3,869.51
E£3,357.12
List Price:  E£4,796.09
You save:  E£1,438.97
ብር9,951.53
List Price:  ብር14,217.09
You save:  ብር4,265.55
Kz64,180.83
List Price:  Kz91,690.83
You save:  Kz27,510
CLP$67,622.76
List Price:  CLP$96,608.09
You save:  CLP$28,985.32
CN¥499.08
List Price:  CN¥713
You save:  CN¥213.92
RD$4,400.31
List Price:  RD$6,286.43
You save:  RD$1,886.11
DA9,080.64
List Price:  DA12,972.90
You save:  DA3,892.26
FJ$157.85
List Price:  FJ$225.51
You save:  FJ$67.66
Q535.60
List Price:  Q765.18
You save:  Q229.57
GY$14,613.01
List Price:  GY$20,876.62
You save:  GY$6,263.61
ISK kr8,563.48
List Price:  ISK kr12,234.07
You save:  ISK kr3,670.58
DH629.93
List Price:  DH899.93
You save:  DH270
L1,164.15
List Price:  L1,663.14
You save:  L498.99
ден3,684.99
List Price:  ден5,264.49
You save:  ден1,579.50
MOP$561.05
List Price:  MOP$801.54
You save:  MOP$240.48
N$1,240.36
List Price:  N$1,772.03
You save:  N$531.66
C$2,571.43
List Price:  C$3,673.64
You save:  C$1,102.20
रु9,854.29
List Price:  रु14,078.16
You save:  रु4,223.87
S/247.31
List Price:  S/353.31
You save:  S/106
K295.70
List Price:  K422.46
You save:  K126.75
SAR262.63
List Price:  SAR375.20
You save:  SAR112.57
ZK1,648.02
List Price:  ZK2,354.41
You save:  ZK706.39
L303.89
List Price:  L434.15
You save:  L130.26
Kč1,464.79
List Price:  Kč2,092.65
You save:  Kč627.85
Ft23,780.13
List Price:  Ft33,973.07
You save:  Ft10,192.94
SEK kr662.21
List Price:  SEK kr946.06
You save:  SEK kr283.84
ARS$93,215
List Price:  ARS$133,170
You save:  ARS$39,954.99
Bs482.84
List Price:  Bs689.81
You save:  Bs206.96
COP$281,463.10
List Price:  COP$402,107.38
You save:  COP$120,644.27
₡35,283.96
List Price:  ₡50,407.82
You save:  ₡15,123.85
L1,829.53
List Price:  L2,613.72
You save:  L784.19
₲505,187.47
List Price:  ₲721,727.32
You save:  ₲216,539.85
$U2,800.03
List Price:  $U4,000.21
You save:  $U1,200.18
zł255.38
List Price:  zł364.85
You save:  zł109.46
Already have an account? Log In

Transcript

Rust guarantees memory safety at compile time. As a part of that variables must be initialized before you can use them. This code won't work because Enigma has been declared but not initialized to a value before we try to use it. In fact, it won't even compile, we get the error, use of possibly uninitialized variable and nygma. What if we might initialize the nygma depending on some condition, the compiler won't reason about the value of a condition at compile time, even if it's a literal, true or false. Conditional evaluation is handled at runtime.

So the compiler can't guarantee that Enigma will be initialized before it is used because the compiler doesn't know what the value of true will be at runtime. So this still won't work. But this works. The compiler can tell the Enigma is guaranteed to be initialized before it is used. As long as As the compiler can guarantee something is safe, it will let you do it. What if you tried the same thing and see what if you declared a variable and then used it before initializing it?

Welcome to the glorious realm of undefined behavior on my Mac. I tried it and then I got the number one. I'm not sure if that's just what happened to me memory or what but your mileage may vary because your compiler can choose to do anything it wants to do in this situation. Let's leave C and its craziness alone. In the next video, we will go over functions

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.