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.84
List Price:  €85.49
You save:  €25.65
£51.88
List Price:  £74.12
You save:  £22.24
CA$95.69
List Price:  CA$136.71
You save:  CA$41.01
A$106.55
List Price:  A$152.23
You save:  A$45.67
S$89.62
List Price:  S$128.04
You save:  S$38.41
HK$549.41
List Price:  HK$784.91
You save:  HK$235.49
CHF 55.73
List Price:  CHF 79.62
You save:  CHF 23.88
NOK kr707.25
List Price:  NOK kr1,010.40
You save:  NOK kr303.15
DKK kr446.59
List Price:  DKK kr638.01
You save:  DKK kr191.42
NZ$116.77
List Price:  NZ$166.83
You save:  NZ$50.05
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,508.66
List Price:  ৳12,155.75
You save:  ৳3,647.09
₹6,017.20
List Price:  ₹8,596.36
You save:  ₹2,579.16
RM297.63
List Price:  RM425.20
You save:  RM127.57
₦107,163.78
List Price:  ₦153,097.68
You save:  ₦45,933.90
₨19,903.40
List Price:  ₨28,434.65
You save:  ₨8,531.25
฿2,266.87
List Price:  ฿3,238.52
You save:  ฿971.65
₺2,813.38
List Price:  ₺4,019.29
You save:  ₺1,205.91
B$389.15
List Price:  B$555.95
You save:  B$166.80
R1,249.89
List Price:  R1,785.64
You save:  R535.74
Лв117.15
List Price:  Лв167.37
You save:  Лв50.21
₩96,620.52
List Price:  ₩138,035.24
You save:  ₩41,414.71
₪235.41
List Price:  ₪336.32
You save:  ₪100.90
₱3,963.39
List Price:  ₱5,662.23
You save:  ₱1,698.83
¥10,302.88
List Price:  ¥14,719.04
You save:  ¥4,416.15
MX$1,307.39
List Price:  MX$1,867.78
You save:  MX$560.39
QR254.80
List Price:  QR364.02
You save:  QR109.21
P934.22
List Price:  P1,334.67
You save:  P400.44
KSh9,042.70
List Price:  KSh12,918.70
You save:  KSh3,876
E£3,461
List Price:  E£4,944.50
You save:  E£1,483.49
ብር9,581.63
List Price:  ብር13,688.63
You save:  ብር4,107
Kz63,827.73
List Price:  Kz91,186.38
You save:  Kz27,358.65
CLP$67,071.41
List Price:  CLP$95,820.41
You save:  CLP$28,749
CN¥501.68
List Price:  CN¥716.71
You save:  CN¥215.03
RD$4,220.39
List Price:  RD$6,029.39
You save:  RD$1,809
DA9,102.91
List Price:  DA13,004.72
You save:  DA3,901.80
FJ$157.10
List Price:  FJ$224.44
You save:  FJ$67.34
Q537.52
List Price:  Q767.92
You save:  Q230.39
GY$14,639.70
List Price:  GY$20,914.76
You save:  GY$6,275.05
ISK kr8,523.38
List Price:  ISK kr12,176.78
You save:  ISK kr3,653.40
DH630.08
List Price:  DH900.15
You save:  DH270.07
L1,185.67
List Price:  L1,693.89
You save:  L508.21
ден3,683.77
List Price:  ден5,262.76
You save:  ден1,578.98
MOP$565.78
List Price:  MOP$808.30
You save:  MOP$242.51
N$1,240.92
List Price:  N$1,772.82
You save:  N$531.90
C$2,572.13
List Price:  C$3,674.63
You save:  C$1,102.50
रु9,597.89
List Price:  रु13,711.85
You save:  रु4,113.96
S/248.21
List Price:  S/354.61
You save:  S/106.39
K288.70
List Price:  K412.45
You save:  K123.75
SAR262.47
List Price:  SAR374.98
You save:  SAR112.50
ZK1,619.84
List Price:  ZK2,314.16
You save:  ZK694.31
L304.09
List Price:  L434.44
You save:  L130.34
Kč1,476.18
List Price:  Kč2,108.91
You save:  Kč632.73
Ft23,949.30
List Price:  Ft34,214.75
You save:  Ft10,265.45
SEK kr671.19
List Price:  SEK kr958.89
You save:  SEK kr287.69
ARS$88,241.78
List Price:  ARS$126,065.09
You save:  ARS$37,823.31
Bs484.56
List Price:  Bs692.26
You save:  Bs207.69
COP$280,918.82
List Price:  COP$401,329.80
You save:  COP$120,410.98
₡35,290.17
List Price:  ₡50,416.70
You save:  ₡15,126.52
L1,844.23
List Price:  L2,634.73
You save:  L790.50
₲542,310.94
List Price:  ₲774,763.12
You save:  ₲232,452.18
$U2,844.98
List Price:  $U4,064.43
You save:  $U1,219.45
zł254.88
List Price:  zł364.14
You save:  zł109.25
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.