为什么使用 `cblas_ccopy` 会导致间歇性内存错误?

Why does using `cblas_ccopy` cause intermittent memory errors?

下面的代码只是尝试使用 cblas_ccopy 将值从一个指针复制到另一个指针,但它会导致大约三分之一的时间出现 malloc: *** error ... incorrect checksum for freed object 错误。为什么它总是不起作用?

import Accelerate

func testCopy() {

    // set capacity
    let capacity: Int = 1000

    // destination array
    let destinationArray = UnsafeMutablePointer<Float>.allocate(capacity: capacity)
    destinationArray.initialize(repeating: 0, count: capacity)

    // source array
    let sourceArray = UnsafeMutablePointer<Float>.allocate(capacity: capacity)
    sourceArray.initialize(repeating: 1, count: capacity)

    // copy values
    cblas_ccopy(Int32(capacity),
                UnsafeRawPointer(sourceArray),
                1,
                UnsafeMutableRawPointer(destinationArray),
                1)

    // check to see if values were copied
    for idx in 0..<capacity {
        print(idx, destinationArray[idx])
    }
}

testCopy()

当运行将此作为单元测试时,错误为objc[44736]: autorelease pool page 0x7fecb903c000 corrupted。当运行它作为脚本时,错误是incorrect checksum

我尝试在 malloc_error_break 中设置断点,但我不明白如何解释输出。

我也尝试将 sourceArraydestinationArray 作为参数传递给 cblas_ccopy,但没有将它们转换为原始指针,但这没有帮助。

_ccopy中的c-前缀表示元素类型是单精度复数。所以,在你的cblas_ccopy(Int32(capacity),...)中,两个指针都需要包含capacity个单精度复数元素,即2 * capacity个单精度浮点数元素。

您只需分配 capacity 个单精度浮点数元素。你可能知道当内存访问超过内存限制时会发生什么。

尝试将分配大小加倍:

let destinationArray = UnsafeMutablePointer<Float>.allocate(capacity: 2 * capacity)
destinationArray.initialize(repeating: 0, count: 2 * capacity)

// source array
let sourceArray = UnsafeMutablePointer<Float>.allocate(capacity: 2 * capacity)
sourceArray.initialize(repeating: 1, count: 2 * capacity)

// copy values
cblas_ccopy(Int32(capacity), //<- do not double here.
            UnsafeRawPointer(sourceArray),
            1,
            UnsafeMutableRawPointer(destinationArray),
            1)

(或者尝试分配单精度 复数 数的 capacity 个元素,而不是 Float。)

使用 cblas_scopy 而不是 cblas_ccopycblas_ccopy 复制(单精度)复杂 数字,其大小是您实际使用的单精度数字的两倍,因此您超出了缓冲区的末尾。