UnsafeMutablePointer<Type>.alloc() 参数有什么意义?
What meaning does the parameter have for UnsafeMutablePointer<Type>.alloc()?
假设我想创建一个指向 Swift 中的 Int
的 指针 。据我所知,我会做 this:
let pointer = UnsafeMutablePointer<Int>.alloc(1)
pointer.memory = 100
println(pointer) //prints 0x00007f8672fb7eb0
println(pointer.memory) //prints 100
现在,当我调用 UnsafeMutablePointer<Int>.alloc(1)
时,1
表示什么?我假设它是从指针地址开始在内存中分配的 Int
s 的数量。
所以1
会分配8字节,2
会分配16字节,等等...
这是真的吗?
如果是,Swift 使用 generic 类型为 UnsafeMutablePointer<Type>
分配多少内存?
Now, when I call UnsafeMutablePointer<Int>.alloc(1)
, what does 1
denote? I assume its the number of Ints allocated in memory starting
from the pointer address.
这是分配内存的项目数。
So 1 would allocate 8 bytes, 2 would allocate 16 bytes, and so on...
Is this true?
差不多。一个 Int
可以是 4 或 8 字节,具体取决于
架构。
If so, how much memory does Swift allocate for
UnsafeMutablePointer<Type>
using a generic type?
不能为泛型类型分配内存。在那个点
alloc()
被调用,类型参数 T
必须已知,或者给定
明确或从上下文推断。
假设我想创建一个指向 Swift 中的 Int
的 指针 。据我所知,我会做 this:
let pointer = UnsafeMutablePointer<Int>.alloc(1)
pointer.memory = 100
println(pointer) //prints 0x00007f8672fb7eb0
println(pointer.memory) //prints 100
现在,当我调用 UnsafeMutablePointer<Int>.alloc(1)
时,1
表示什么?我假设它是从指针地址开始在内存中分配的 Int
s 的数量。
所以1
会分配8字节,2
会分配16字节,等等...
这是真的吗?
如果是,Swift 使用 generic 类型为 UnsafeMutablePointer<Type>
分配多少内存?
Now, when I call
UnsafeMutablePointer<Int>.alloc(1)
, what does 1 denote? I assume its the number of Ints allocated in memory starting from the pointer address.
这是分配内存的项目数。
So 1 would allocate 8 bytes, 2 would allocate 16 bytes, and so on... Is this true?
差不多。一个 Int
可以是 4 或 8 字节,具体取决于
架构。
If so, how much memory does Swift allocate for
UnsafeMutablePointer<Type>
using a generic type?
不能为泛型类型分配内存。在那个点
alloc()
被调用,类型参数 T
必须已知,或者给定
明确或从上下文推断。