"Rust의 uninitialized와 forget"의 두 판 사이의 차이

76번째 줄: 76번째 줄:
 
[[파일:캡처226.PNG]]
 
[[파일:캡처226.PNG]]
  
보다시피 메모리가 줄어드는 것을 확인할 수 있다.
+
보다시피 메모리가 줄어 들지 않는 것을 확인할 수 있다.

2018년 11월 14일 (수) 13:11 판

uninitialized

러스트는 객체의 초기화, 혹은 객체를 참조하기 전에 객체를 생성하는 것을 강제한다. 그렇게 해야 초보자가 하기 쉬운 쓰레기값을 가진 객체를 사용하는 실수를 방지하기 위해서이다.

하지만 항상 그렇게 하는 것은 피곤할 수 있고, 초기화를 안해도 되는 객체 또한 있기 마련이다. 이를 위해서 Rust에서는 uninitialized함수를 제공한다.

fn main() {
    let num = unsafe{std::mem::uninitialized::<i32>()};
    println!("{}",num);
}

unitialized는 Rust 메모리 안전 관리 수칙에서 벗어나는 행위이기 때문에 '너 정말 이게 안전하지 않은 지 아니?라는 확인용인 unsafe구문 내에서만 사용할 수 있다. 이 코드를 실행하면 num의 값이 쓰레기 값인 걸 확인할 수 있다.

    Finished dev [unoptimized + debuginfo] target(s) in 0.41s
     Running `target\debug\tes.exe`
0x00000086
PS C:\Users\qwead\Documents\tes> cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\tes.exe`
0x00000098
PS C:\Users\qwead\Documents\tes> cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\tes.exe`
0x000000f4
PS C:\Users\qwead\Documents\tes> cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target\debug\tes.exe`
0x0000008b

forget

uninitialized가 객체를 생성할 때 Rust 메모리 관리 규칙을 벗어나는 방법을 제공한다면 forget은 객체를 삭제할 때 Rust 메모리 관리 규칙에서 벗어나는 방법을 제공한다.

Rust의 메모리 관리 규칙은 스코프가 끝날 때 그 스코프가 소유한 객체도 메모리를 해제한다. 이때 일반적인 객체가 아닌 파일IO관련 객체 혹은 동적메모리를 사용하는 객체의 경우 객체가 죽을 때 객체의 drop메소드가 호출되어 객체 자신이 정리할 시간을 제공한다.

하지만 forget은 drop메소드를 호출하지 않고 객체를 해제한다.

fn main() {
    {
        let mut s = Vec::<u128>::new();
        for _ in 0..std::u16::MAX as u32 * 16{
            s.push(0u128);
        }
        let mut n = String::new();
        println!("press any key");
        std::io::stdin().read_line(&mut n);
    }
    let mut n = String::new();
    println!("press any key");
    std::io::stdin().read_line(&mut n);
}

캡처223.PNG

캡처224.PNG

메모리를 정상적으로 확인하는 것을 볼 수 있다. 하지만 코드 중간에 forget함수를 삽입하여 drop메소드를 안 호출하고 객체를 제거하면

fn main() {
    {
        let mut s = Vec::<u128>::new();
        for _ in 0..std::u16::MAX as u32 * 16{
            s.push(0u128);
        }
        let mut n = String::new();
        println!("press any key");
        std::io::stdin().read_line(&mut n);
        std::mem::forget(s);
    }
    let mut n = String::new();
    println!("press any key");
    std::io::stdin().read_line(&mut n);
}

캡처225.PNG

캡처226.PNG

보다시피 메모리가 줄어 들지 않는 것을 확인할 수 있다.