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

标签 swift accelerate-framework unsafe-pointers cblas

下面的代码只是尝试使用 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,但没有将它们转换为原始指针,但这没有帮助。

最佳答案

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

关于swift - 为什么使用 `cblas_ccopy` 会导致间歇性内存错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441635/

相关文章:

ios - 如何使用 CocoaPods 分发测试替身?

ios - 从框架加载 Storyboard

swift - 如何使用 OpaquePointer 调用 DSP 函数?

ios - 我应该在vdsp_mmul还是cblas_dgemm之间选择哪个?

iphone - 使用 Accelerate 框架对无符号 8 位整数数组求和

ios - Swift 3 编译器错误 : 'bytes' is unavailable: use withUnsafeBytes instead

ios - super View 中的中心 View

swift3 - Swift 3 UnsafePointer($0) 不再在 Xcode 8 beta 6 中编译

rust - Rust:如何告诉借位检查员此举取决于 bool 值?

swift - 使用 Swift 打开(交互式)终端