6

I'm on Swift 3, and I need to interact with an C API, which accepts a NULL-terminated list of strings, for example

const char *cmd[] = {"name1", "value1", NULL};
command(cmd);

In Swift, the API was imported like

func command(_ args: UnsafeMutablePointer<UnsafePointer<Int8>?>!)

After trying hundreds of times using type casting or unsafeAddress(of:) I still cannot get this work. Even though I pass a valid pointer that passed compilation, it crashes at runtime saying invalid memory access (at strlen function). Or maybe it's something about ARC?

let array = ["name1", "value1", nil]

// ???
// args: UnsafeMutablePointer<UnsafePointer<Int8>?>

command(args)
jscs
  • 63,694
  • 13
  • 151
  • 195
00007chl
  • 91
  • 1
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/29469158/how-to-pass-an-array-of-swift-strings-to-a-c-function-taking-a-char-parameter – Vini App Oct 17 '17 at 18:59

2 Answers2

9

You can proceed similarly as in How to pass an array of Swift strings to a C function taking a char ** parameter. It is a bit different because of the different const-ness of the argument array, and because there is a terminating nil (which must not be passed to strdup()).

This is how it should work:

let array: [String?] = ["name1", "name2", nil]

// Create [UnsafePointer<Int8>]:
var cargs = array.map { $0.flatMap { UnsafePointer<Int8>(strdup($0)) } }
// Call C function:
let result = command(&cargs)
// Free the duplicated strings:
for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) }
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks! Works like a charm :] – 00007chl Jul 09 '16 at 16:48
  • while invoking command API i'm getting error...Use of unresolved identifier 'command'; did you mean 'doCommand'? – Sunil aruru Sep 24 '18 at 11:09
  • 1
    @Sunilaruru: The question was about calling an *existing* C function `command`. – Martin R Sep 24 '18 at 11:26
  • var tool = "/bin/rm" let array: [String?] = ["name1", "name2", nil] // Create [UnsafePointer]: var cargs = array.map { $0.flatMap { UnsafePointer(strdup($0)) } } // Call C function: let result = command(&cargs) // Free the duplicated strings: for ptr in cargs { free(UnsafeMutablePointer(mutating: ptr)) } var pipe: FILE? = nil status = AuthorizationExecuteWithPrivileges(authRef!, tool, authFlags, cargs, &pipe); is it ok? – Sunil aruru Sep 24 '18 at 12:31
0

This class provides a pointer that works with char** and automatically deallocates the memory, even though it's a struct (using a little trick with a mapped data with deallocator).

public struct CStringArray {
    public let pointer: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>
    public let count: Int
    private var data: Data

    public init(_ array: [String]) {
        let count = array.count

        // Allocate memory to hold the CStrings and a terminating nil
        let pointer = UnsafeMutablePointer<UnsafeMutablePointer<CChar>?>.allocate(capacity: count + 1)
        pointer.initialize(repeating: nil, count: count + 1)  // Implicit terminating nil at the end of the array

        // Populate the allocated memory with pointers to CStrings
        var e = 0
        array.forEach {
            pointer[e] = strdup($0)
            e += 1
        }

        // This uses the deallocator available on the data structure as a solution to the fact that structs do not have `deinit`
        self.data = Data(bytesNoCopy: pointer, count: MemoryLayout<UnsafeMutablePointer<CChar>>.size * count, deallocator: .custom({_,_ in
            for i in 0...count - 1 {
                free(pointer[i])
            }
            pointer.deallocate()
        }))

        self.pointer = pointer
        self.count = array.count
    }

    public subscript(index: Data.Index) -> UnsafeMutablePointer<CChar>? {
        get {
            precondition(index >= 0 && index < count, "Index out of range")
            return pointer[index]
        }
    }

    public subscript(index: Data.Index) -> String? {
        get {
            precondition(index >= 0 && index < count, "Index out of range")
            if let pointee = pointer[index] {
                return String(cString: pointee)
            }

            return nil
        }
    }
}
RopeySim
  • 423
  • 4
  • 11