rust [rust] Rust 요약(메모리 관리)
2023.05.10 18:28
[rust] Rust 요약(메모리 관리)
Rust 요약(메모리 관리)
Rust 는 꽤 독특한 메모리 관리 시스템을 가지고 있다.
소유권
소유권 참조
메모리 할당
/// A macro similar to `vec![$elem; $size]` which returns a boxed array.
///
/// ```rustc
/// let _: Box<[u8; 1024]> = box_array![0; 1024];
/// ```
macro_rules! box_array {
($val:expr ; $len:expr) => {{
// Use a generic function so that the pointer cast remains type-safe
fn vec_to_boxed_array<T>(vec: Vec<T>) -> Box<[T; $len]> {
let boxed_slice = vec.into_boxed_slice();
let ptr = ::std::boxed::Box::into_raw(boxed_slice) as *mut [T; $len];
unsafe { Box::from_raw(ptr) }
}
vec_to_boxed_array(vec![$val; $len])
}};
}
fn main() {
const LEN: usize = 10_000_000;
let _huge_heap_array: Box<[u8; LEN]> = box_array![0; LEN];
}
[출처] https://www.skyer9.pe.kr/wordpress/?p=5636
본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.