@@ -6,7 +6,7 @@ of bits that may or may not even reflect a valid state for the type that is
6
6
supposed to inhabit that location of memory. Attempting to interpret this memory
7
7
as a value of * any* type will cause Undefined Behaviour. Do Not Do This.
8
8
9
- Like C, all stack variables in Rust begin their life as uninitialized until a
9
+ Like C, all stack variables in Rust are uninitialized until a
10
10
value is explicitly assigned to them. Unlike C, Rust statically prevents you
11
11
from ever reading them until you do:
12
12
@@ -32,17 +32,14 @@ or anything like that. So this compiles:
32
32
``` rust
33
33
fn main () {
34
34
let x : i32 ;
35
- let y : i32 ;
36
-
37
- y = 1 ;
38
35
39
36
if true {
40
37
x = 1 ;
41
38
} else {
42
39
x = 2 ;
43
40
}
44
41
45
- println! (" {} {} " , x , y );
42
+ println! (" {}" , x );
46
43
}
47
44
```
48
45
@@ -98,13 +95,13 @@ to call the destructor of a variable that is conditionally initialized? It turns
98
95
out that Rust actually tracks whether a type should be dropped or not * at
99
96
runtime* . As a variable becomes initialized and uninitialized, a * drop flag* for
100
97
that variable is set and unset. When a variable goes out of scope or is assigned
101
- it evaluates whether the current value of the variable should be dropped. Of
102
- course, static analysis can remove these checks. If the compiler can prove that
98
+ a value, it evaluates whether the current value of the variable should be dropped.
99
+ Of course, static analysis can remove these checks. If the compiler can prove that
103
100
a value is guaranteed to be either initialized or not, then it can theoretically
104
101
generate more efficient code! As such it may be desirable to structure code to
105
102
have * static drop semantics* when possible.
106
103
107
- As of Rust 1.0, the drop flags are actually not-so-secretly stashed in a secret
104
+ As of Rust 1.0, the drop flags are actually not-so-secretly stashed in a hidden
108
105
field of any type that implements Drop. The language sets the drop flag by
109
106
overwriting the entire struct with a particular value. This is pretty obviously
110
107
Not The Fastest and causes a bunch of trouble with optimizing code. As such work
@@ -115,7 +112,7 @@ requires fairly substantial changes to the compiler.
115
112
So in general, Rust programs don't need to worry about uninitialized values on
116
113
the stack for correctness. Although they might care for performance. Thankfully,
117
114
Rust makes it easy to take control here! Uninitialized values are there, and
118
- Safe Rust lets you work with them, but you're never in trouble .
115
+ Safe Rust lets you work with them, but you're never in danger .
119
116
120
117
One interesting exception to this rule is working with arrays. Safe Rust doesn't
121
118
permit you to partially initialize an array. When you initialize an array, you
@@ -125,23 +122,23 @@ Unfortunately this is pretty rigid, especially if you need to initialize your
125
122
array in a more incremental or dynamic way.
126
123
127
124
Unsafe Rust gives us a powerful tool to handle this problem:
128
- ` std:: mem::uninitialized` . This function pretends to return a value when really
125
+ ` mem::uninitialized ` . This function pretends to return a value when really
129
126
it does nothing at all. Using it, we can convince Rust that we have initialized
130
127
a variable, allowing us to do trickier things with conditional and incremental
131
128
initialization.
132
129
133
- Unfortunately, this raises a tricky problem . Assignment has a different meaning
134
- to Rust based on whether it believes that a variable is initialized or not. If
135
- it's uninitialized, then Rust will semantically just memcopy the bits over the
136
- uninit ones, and do nothing else. However if Rust believes a value to be
137
- initialized, it will try to ` Drop ` the old value! Since we've tricked Rust into
138
- believing that the value is initialized, we can no longer safely use normal
139
- assignment.
130
+ Unfortunately, this opens us up to all kinds of problems . Assignment has a
131
+ different meaning to Rust based on whether it believes that a variable is
132
+ initialized or not. If it's uninitialized, then Rust will semantically just
133
+ memcopy the bits over the uninitialized ones, and do nothing else. However if Rust
134
+ believes a value to be initialized, it will try to ` Drop ` the old value!
135
+ Since we've tricked Rust into believing that the value is initialized, we
136
+ can no longer safely use normal assignment.
140
137
141
- This is also a problem if you're working with a raw system allocator, which of
142
- course returns a pointer to uninitialized memory.
138
+ This is also a problem if you're working with a raw system allocator, which
139
+ returns a pointer to uninitialized memory.
143
140
144
- To handle this, we must use the ` std:: ptr` module. In particular, it provides
141
+ To handle this, we must use the ` ptr ` module. In particular, it provides
145
142
three functions that allow us to assign bytes to a location in memory without
146
143
evaluating the old value: ` write ` , ` copy ` , and ` copy_nonoverlapping ` .
147
144
@@ -157,7 +154,7 @@ evaluating the old value: `write`, `copy`, and `copy_nonoverlapping`.
157
154
It should go without saying that these functions, if misused, will cause serious
158
155
havoc or just straight up Undefined Behaviour. The only things that these
159
156
functions * themselves* require is that the locations you want to read and write
160
- are allocated. However the ways writing arbitrary bit patterns to arbitrary
157
+ are allocated. However the ways writing arbitrary bits to arbitrary
161
158
locations of memory can break things are basically uncountable!
162
159
163
160
Putting this all together, we get the following:
@@ -177,6 +174,7 @@ fn main() {
177
174
x = mem :: uninitialized ();
178
175
for i in 0 .. SIZE {
179
176
// very carefully overwrite each index without reading it
177
+ // NOTE: exception safety is not a concern; Box can't panic
180
178
ptr :: write (& mut x [i ], Box :: new (i ));
181
179
}
182
180
}
@@ -186,15 +184,16 @@ fn main() {
186
184
```
187
185
188
186
It's worth noting that you don't need to worry about ptr::write-style
189
- shenanigans with Plain Old Data (POD; types which don't implement Drop, nor
190
- contain Drop types) , because Rust knows not to try to Drop them. Similarly you
191
- should be able to assign the POD fields of partially initialized structs
192
- directly.
187
+ shenanigans with types which don't implement Drop or
188
+ contain Drop types, because Rust knows not to try to Drop them. Similarly you
189
+ should be able to assign to fields of partially initialized structs
190
+ directly if those fields don't contain any Drop types .
193
191
194
- However when working with uninitialized memory you need to be ever vigilant for
192
+ However when working with uninitialized memory you need to be ever- vigilant for
195
193
Rust trying to Drop values you make like this before they're fully initialized.
196
- So every control path through that variable's scope must initialize the value
197
- before it ends. * This includes code panicking* . Again, POD types need not worry.
194
+ Every control path through that variable's scope must initialize the value
195
+ before it ends, if has a destructor.
196
+ * [ This includes code panicking] ( unwinding.html ) * .
198
197
199
198
And that's about it for working with uninitialized memory! Basically nothing
200
199
anywhere expects to be handed uninitialized memory, so if you're going to pass
0 commit comments