在C中我会做这样的事情
parallels@ubuntu:~/Git$ /home/parallels/Git/build/tools/releasetools/ota_from_target_files /home/parallels/Git/tmp/ota-dest.zip /home/parallels/Git/tmp/ota_update.zip
unzipping target target-files...
(using device-specific extensions from target_files)
--- can't determine the cache partition size ---
loaded device-specific extensions from /tmp/targetfiles-hVvBNo/META/releasetools.py
using prebuilt recovery.img from IMAGES...
using prebuilt boot.img from IMAGES...
Traceback (most recent call last):
File "/home/parallels/Git/build/tools/releasetools/ota_from_target_files", line 1801, in <module>
main(sys.argv[1:])
File "/home/parallels/Git/build/tools/releasetools/ota_from_target_files", line 1757, in main
WriteFullOTAPackage(input_zip, output_zip)
File "/home/parallels/Git/build/tools/releasetools/ota_from_target_files", line 643, in WriteFullOTAPackage
recovery_img, boot_img)
File "/home/parallels/Git/build/tools/releasetools/common.py", line 1491, in MakeRecoveryPatch
_, _, patch = d.ComputePatch()
File "/home/parallels/Git/build/tools/releasetools/common.py", line 1151, in ComputePatch
p = Run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
File "/home/parallels/Git/build/tools/releasetools/common.py", line 87, in Run
return subprocess.Popen(args, **kwargs)
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我见过像这样使用的UnsafeMutablePointer
int count = 10;
int *buffer;
num = malloc(count * sizeof(int));
for (int i = 0; i < count; i++) {
buffer[i] = rand();
}
如何在Swift中为C风格缓冲区使用UnsafeMutableBufferPointer?另外,我如何为指针重新分配更多空间?
答案 0 :(得分:8)
UnsafeMutableBufferPointer
没有自己的内存,因此您仍然必须使用UnsafeMutablePointer
来分配底层内存。但是,您可以将缓冲区指针用作集合,并使用for-in循环对其进行枚举。
let count = 50
let ptr = UnsafeMutablePointer<Int>.alloc(count)
let buffer = UnsafeMutableBufferPointer(start: ptr, count: count)
for (i, _) in buffer.enumerate() {
buffer[i] = Int(arc4random())
}
// Do stuff...
ptr.dealloc(count) // Don't forget to dealloc!
Swift指针不提供realloc
功能。您可以使用C函数,或者如果您愿意,可以使用自己的函数。