gpt4 book ai didi

swift - 将指针元组转换为指针指针

转载 作者:搜寻专家 更新时间:2023-10-31 22:57:11 24 4
gpt4 key购买 nike

我有一段C代码:

#define AV_NUM_DATA_POINTERS 8
uint8_t *data[AV_NUM_DATA_POINTERS];

在快速生成的界面中,它看起来像:

var data: (UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?)

有一个函数需要这个data 变量。它在 C 中的表示如下:

int sws_scale(..., const uint8_t *const srcSlice[], ...);

但是 swift 会生成以下签名:

func sws_scale(..., _ srcSlice: UnsafePointer<UnsafePointer<UInt8>?>!, ...) -> Int32

将巨大的元组转换为适用于 sws_scale 函数的类型的最佳方法是什么?

最佳答案

示例来自 Swift.org - UnsafeRawPointer Migration :

var bytes: (CChar, CChar, CChar, CChar) = (0x61, 0x62, 0x63, 0)
let name: String = withUnsafePointer(to: &bytes) { ptr -> String in
return String(cString: UnsafeRawPointer(ptr).assumingMemoryBound(to: CChar.self))
}

应用于 sws_scale:

var data: (UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?, UnsafeMutablePointer<UInt8>?)
withUnsafePointer(to: data) { dataPointer in
let dataPointer =
UnsafeRawPointer(dataPointer)
.assumingMemoryBound(to: UnsafePointer<UInt8>?.self)

sws_scale(…, dataPointer, …)
}

请勿执行以下操作:

withUnsafePointer(to: &data.0)

在优化构建中使用上述内容时,我遇到了内存对齐问题。

关于swift - 将指针元组转换为指针指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44201965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com