首先,我知道我可以使用Swift Array,但我不会拥有内存。阵列的确如此。我不想分配自己的数组。为此编写一个类我需要一个deinit来在对象完成时释放我分配的内存。所以我写了下面的测试代码。我想知道释放这种记忆的正确方法。我注意到缓冲区是一个真正的对象,并具有deinitialize和dealloc。我也注意到我可以释放它。关注免费我可能会释放对象并将内存留作孤儿。无论如何,我想我会看到其他人的想法。
import Foundation
import Accelerate
let buffer:UnsafeMutablePointer<Float>
buffer = UnsafeMutablePointer<Float>.allocate(capacity: 100)
var s = Float(1.0)
var i = Float(1.0)
vDSP_vramp(&s, &i, buffer, 1, 100)
print("after positive ramp stride one")
for i in 0..<10{
print(buffer[i])
}
s = -1.0; i = -1.0
vDSP_vramp(&s, &i, buffer, 2, 50)
print("after negative ramp stride two")
print("result of print with stride one")
for i in 0..<10{
print(buffer[i])
}
print("print result with stride 2")
for i in 0..<10{
print(buffer[2 * i])
}
print()
buffer.deinitialize()
print("after deinitialize")
print(buffer[1])
free(buffer)
print("after free")
print(buffer[1])
输出看起来像
after positive ramp stride one
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
after negative ramp stride two
result of print with stride one
-1.0
2.0
-2.0
4.0
-3.0
6.0
-4.0
8.0
-5.0
10.0
print result with stride 2
-1.0
-2.0
-3.0
-4.0
-5.0
-6.0
-7.0
-8.0
-9.0
-10.0
after deinitialize
2.0
after free
0.0
Program ended with exit code: 0