Correct way to call "realloc" in Swift with a Float array? -


I'm trying to say what size is to send "realloc" when I call through Swift It seems that I want to add an extra byte , but I do not understand why

  typiaas floats = unsafepointant & lt; Float & gt; Let's FLOAT_SIZE = sizeof (float) func floats_realloc (floats: floats, qty_of_floats: int) - & gt; Floats {let KLUDGE = 1 // Why? Go qty_of_bytes = (qty_of_floats * FLOAT_SIZE) + kludge go realloced_floats = floats (realloc (floats, uint (qty_of_bytes))) return realloced_floats}  

If I set the 0 kludge here, This is what happens when I try to make room for a new member in a three-member array:

In: [0.9, 0.9, 0.9]

Out: <[0.0, 0.0, 0.9, 0.0]

What do I expect:

Out: [0.9, 0.9, 0.9 , 0.0]

The arrays I send are used in SWIFT And

  var foo_floats = Floats.alloc (QTY_OF_FLOATS)  
What's wrong with

My call to call again?

I have discussed this on Apple's developer forum. It has been learned that using the Swift Olock Oles Space for a Swift array, not a C array. So you want to use MutableUnsafePointer for a C array and "realloc" (C to C), you want to stick with C functions, like "malloc" (C-P).

By adding the following function, and when I initially set up my floats array, the "reallock" bug went:

  func floats_alloc (qty_of_floats: int ) - & gt; Floating {// Return Floats.alloc (qty_of_floats) Go qty_of_bytes = (qty_of_floats * FLOAT_SIZE) alloced_floats = floating (malloc (utyt (qty_of_bytes)) return alloced_floats go}}  

I have tested Now for a few weeks, and all is well.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -