【问题标题】:Why does the documentation of std::ptr::read state that ptr::copy_nonoverlapping "could trigger undefined behavior"?为什么 std::ptr::read 的文档指出 ptr::copy_nonoverlapping “可能触发未定义的行为”?
【发布时间】:2021-12-28 11:18:36
【问题描述】:

std::ptr::read的文档有这个示例代码:

手动实现mem::swap

use std::ptr;

fn swap<T>(a: &mut T, b: &mut T) {
    unsafe {
        // Create a bitwise copy of the value at `a` in `tmp`.
        let tmp = ptr::read(a);

        // Exiting at this point (either by explicitly returning or by
        // calling a function which panics) would cause the value in `tmp` to
        // be dropped while the same value is still referenced by `a`. This
        // could trigger undefined behavior if `T` is not `Copy`.

        // Create a bitwise copy of the value at `b` in `a`.
        // This is safe because mutable references cannot alias.
        ptr::copy_nonoverlapping(b, a, 1);

        // As above, exiting here could trigger undefined behavior because
        // the same value is referenced by `a` and `b`.

        // Move `tmp` into `b`.
        ptr::write(b, tmp);

        // `tmp` has been moved (`write` takes ownership of its second argument),
        // so nothing is dropped implicitly here.
    }
}

let mut foo = "foo".to_owned();
let mut bar = "bar".to_owned();

swap(&mut foo, &mut bar);

assert_eq!(foo, "bar");
assert_eq!(bar, "foo");

我关心这部分:

    ptr::copy_nonoverlapping(b, a, 1);

    // As above, exiting here could trigger undefined behavior because
    // the same value is referenced by `a` and `b`.

在我看来,a: &amp;mut Tb: &amp;mut T 指向两个不同的内存块。为什么说“ab引用了相同的值”,然后触发未定义的行为?我错过了什么吗?

【问题讨论】:

    标签: memory rust


    【解决方案1】:

    我错过了一个重要的注释:

    // Create a bitwise copy of the value at `a` in `tmp`.
    let tmp = ptr::read(a);
    
    // #################### Marked by me
    // Exiting at this point (either by explicitly returning or by
    // calling a function which panics) would cause the value in `tmp` to
    // be dropped while the same value is still referenced by `a`. This
    // could trigger undefined behavior if `T` is not `Copy`.
    // #################### Marked by me
    

    它正在对T 类型的一些数据进行操作,这些数据可能持有托管资源(例如堆上的内存、文件句柄等)而不是Copy(例如String),所以这个按位复制操作会导致两个T 数据持有者共享公共资源,这可能会触发未定义的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2023-03-15
      • 2021-06-25
      • 2019-09-28
      • 1970-01-01
      • 2019-05-21
      • 2018-04-09
      相关资源
      最近更新 更多