1
Fork 0

Box large array to avoid blowing the stack

This commit is contained in:
Lee Bousfield 2017-07-08 13:02:52 -04:00
parent afed75a2d9
commit da81a33b05
No known key found for this signature in database
GPG key ID: C41F6504C1164209

View file

@ -9,6 +9,7 @@
// except according to those terms. // except according to those terms.
#![feature(attr_literals)] #![feature(attr_literals)]
#![feature(repr_align)] #![feature(repr_align)]
#![feature(box_syntax)]
use std::mem; use std::mem;
@ -201,13 +202,14 @@ pub fn main() {
assert_eq!(mem::size_of_val(&a), 16); assert_eq!(mem::size_of_val(&a), 16);
assert!(is_aligned_to(&a, 16)); assert!(is_aligned_to(&a, 16));
let mut arr = [0; 0x10000]; let mut large = box AlignLarge {
arr[0] = 132; stuff: [0; 0x10000],
let large = AlignLarge {
stuff: arr,
}; };
large.stuff[0] = 132;
*large.stuff.last_mut().unwrap() = 102;
assert_eq!(large.stuff[0], 132); assert_eq!(large.stuff[0], 132);
assert_eq!(large.stuff.last(), Some(&102));
assert_eq!(mem::align_of::<AlignLarge>(), 0x10000); assert_eq!(mem::align_of::<AlignLarge>(), 0x10000);
assert_eq!(mem::align_of_val(&large), 0x10000); assert_eq!(mem::align_of_val(&*large), 0x10000);
assert!(is_aligned_to(&large, 0x10000)); assert!(is_aligned_to(&*large, 0x10000));
} }