Richtige Methode zum Aufrufen von "realloc" in Swift mit einem Float-Array?
Ich versuche herauszufinden, welche Größe "realloc" gesendet werden soll, wenn ich es über Swift anrufe.Es scheint, dass ich ein zusätzliches Byte hinzufügen muss, aber ich verstehe nicht warum.
typealias Floats = UnsafeMutablePointer<Float>
let FLOAT_SIZE = sizeof( Float )
func floats_realloc( floats:Floats, qty_of_floats:Int ) -> Floats {
let KLUDGE = 1 // Why?
let qty_of_bytes = ( qty_of_floats * FLOAT_SIZE ) + KLUDGE
let realloced_floats = Floats(
realloc( floats, UInt( qty_of_bytes ) )
)
return realloced_floats
}
Wenn ich KLUDGE hier auf 0 setze, passiert Folgendes, wenn ich versuche, Platz für ein neues Mitglied in einem Array mit drei Mitgliedern zu schaffen:
Im:[0.9, 0.9, 0.9]
Aus:[0.0, 0.0, 0.9, 0.0]
Was ich erwarten würde, ist:
Aus:[0,9, 0,9, 0,9, 0,0]
Die Arrays, die ich sende, werden mit Swift erstellt
var foo_floats = Floats.alloc(QTY_OF_FLOATS)
Was stimmt nicht mit meinem Anruf zur Neuzuweisung?