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
€62
List Price:  €88.58
You save:  €26.57
£52.77
List Price:  £75.39
You save:  £22.61
CA$97.11
List Price:  CA$138.74
You save:  CA$41.62
A$109.06
List Price:  A$155.80
You save:  A$46.74
S$90.77
List Price:  S$129.67
You save:  S$38.90
HK$543.96
List Price:  HK$777.12
You save:  HK$233.16
CHF 57.81
List Price:  CHF 82.59
You save:  CHF 24.78
NOK kr724.48
List Price:  NOK kr1,035.02
You save:  NOK kr310.53
DKK kr462.61
List Price:  DKK kr660.90
You save:  DKK kr198.29
NZ$118.07
List Price:  NZ$168.68
You save:  NZ$50.60
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,531.88
List Price:  ৳12,188.92
You save:  ৳3,657.04
₹5,991.37
List Price:  ₹8,559.47
You save:  ₹2,568.10
RM299.55
List Price:  RM427.95
You save:  RM128.40
₦112,653.10
List Price:  ₦160,939.90
You save:  ₦48,286.80
₨19,730.13
List Price:  ₨28,187.10
You save:  ₨8,456.97
฿2,297.52
List Price:  ฿3,282.32
You save:  ฿984.79
₺2,704.32
List Price:  ₺3,863.49
You save:  ₺1,159.16
B$402.08
List Price:  B$574.43
You save:  B$172.34
R1,272.71
List Price:  R1,818.24
You save:  R545.52
Лв120.90
List Price:  Лв172.73
You save:  Лв51.82
₩98,081.71
List Price:  ₩140,122.74
You save:  ₩42,041.02
₪250.39
List Price:  ₪357.72
You save:  ₪107.32
₱3,894.10
List Price:  ₱5,563.24
You save:  ₱1,669.13
¥10,133.71
List Price:  ¥14,477.35
You save:  ¥4,343.64
MX$1,370.29
List Price:  MX$1,957.65
You save:  MX$587.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P953.17
List Price:  P1,361.73
You save:  P408.56
KSh9,053.20
List Price:  KSh12,933.70
You save:  KSh3,880.50
E£3,544.03
List Price:  E£5,063.12
You save:  E£1,519.08
ብር9,421.13
List Price:  ብር13,459.34
You save:  ብር4,038.20
Kz63,830.88
List Price:  Kz91,190.88
You save:  Kz27,360
CLP$66,198.64
List Price:  CLP$94,573.54
You save:  CLP$28,374.90
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,131.65
List Price:  RD$5,902.61
You save:  RD$1,770.96
DA9,304.36
List Price:  DA13,292.52
You save:  DA3,988.15
FJ$158.10
List Price:  FJ$225.87
You save:  FJ$67.76
Q539.19
List Price:  Q770.30
You save:  Q231.11
GY$14,646.40
List Price:  GY$20,924.33
You save:  GY$6,277.92
ISK kr9,084
List Price:  ISK kr12,977.70
You save:  ISK kr3,893.70
DH648.26
List Price:  DH926.13
You save:  DH277.86
L1,196.51
List Price:  L1,709.38
You save:  L512.86
ден3,792.14
List Price:  ден5,417.58
You save:  ден1,625.43
MOP$560.81
List Price:  MOP$801.19
You save:  MOP$240.38
N$1,306.98
List Price:  N$1,867.19
You save:  N$560.21
C$2,583.28
List Price:  C$3,690.56
You save:  C$1,107.28
रु9,492.56
List Price:  रु13,561.38
You save:  रु4,068.82
S/257.39
List Price:  S/367.72
You save:  S/110.32
K284.03
List Price:  K405.78
You save:  K121.74
SAR262.52
List Price:  SAR375.04
You save:  SAR112.52
ZK1,947.17
List Price:  ZK2,781.79
You save:  ZK834.62
L317.66
List Price:  L453.82
You save:  L136.16
Kč1,546.29
List Price:  Kč2,209.09
You save:  Kč662.79
Ft25,170.54
List Price:  Ft35,959.45
You save:  Ft10,788.91
SEK kr676.33
List Price:  SEK kr966.22
You save:  SEK kr289.89
ARS$80,137.84
List Price:  ARS$114,487.54
You save:  ARS$34,349.69
Bs485.21
List Price:  Bs693.19
You save:  Bs207.97
COP$303,228.71
List Price:  COP$433,202.44
You save:  COP$129,973.73
₡35,459.16
List Price:  ₡50,658.12
You save:  ₡15,198.95
L1,821.77
List Price:  L2,602.65
You save:  L780.87
₲562,262.91
List Price:  ₲803,267.16
You save:  ₲241,004.24
$U2,923.98
List Price:  $U4,177.30
You save:  $U1,253.31
zł264.70
List Price:  zł378.16
You save:  zł113.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.