I'm trying figure out what size to send "realloc" when I call it through Swift. It seems that I have to add an extra byte, but I don't understand why.
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
}
If I set KLUDGE to 0 here, this is what happens when I attempt to make room for one new member in a three member array:
In: [0.9, 0.9, 0.9]
Out: [0.0, 0.0, 0.9, 0.0]
What I would expect is:
Out: [0.9, 0.9, 0.9, 0.0]
The arrays I'm sending it are created within Swift, using
var foo_floats = Floats.alloc(QTY_OF_FLOATS)
What's wrong with my call to realloc?