0

在 Swift 和 C 之间传递字符串时,我看到一些我不理解的行为。考虑以下 Swift 函数:

func demo()
{
    print("\n\n\n\n")                                                            // Line A

    let str = "thisisastring"
    let strptr = UnsafePointer<Int8>(str)        
    let strptr_cstring = String(cString: strptr)
    print("from swift: str = '\(str)'")
    print("from swift: strptr = \(strptr)")
    print("from swift: strptr.pointee = \(strptr.pointee)")
    print("from swift: strptr_cstring = '\(strptr_cstring)'")
    print("from swift: String(cString: strptr) = '\(String(cString: strptr))'")

    let ret_strptr = return_string(str)!                                        // Line B1
    //let ret_strptr = return_string(strptr)!                                   // Line B2
    //let ret_strptr = strptr                                                   // Line B3

    print("from swift: ret_strptr = \(ret_strptr) ")
    print("from swift: ret_strptr.pointee = \(ret_strptr.pointee)")
    let ret_strptr_cstring = String(cString: ret_strptr)
    print("from swift: ret_strptr_cstring = '\(ret_strptr_cstring)'")
    print("from swift: String(cString: ret_strptr) = '\(String(cString: ret_strptr))'")
}

和 C 函数:

const char *return_string(const char *c)
{
    printf("from c: got pointer %p = %s\n",c,c);
    return c;
}

如果我demo按照上面的方式运行(所以只有 B2 和 B3 行被注释掉),我得到以下输出:

from swift: str = 'thisisastring'
from swift: strptr = 0x000000010021b8a0
from swift: strptr.pointee = 116
from swift: strptr_cstring = 'thisisastring'
from swift: String(cString: strptr) = 'thisisastring'
from c: got pointer 0x10021b9b0 = thisisastring
from swift: ret_strptr = 0x000000010021b9b0 
from swift: ret_strptr.pointee = 102
from swift: ret_strptr_cstring = '102m swift: ret_'
from swift: String(cString: ret_strptr) = 'P�!'

有两件事让我感到惊讶:1) C 代码获取指针 0x10021b9b0,而在 Swift 中,字符串存储在 0x000000010021b8a0。我本来希望它们是相同的,但我猜 Swift 在将它传递给 C 之前会复制字符串?2) 更令人惊讶的是,虽然从 C 返回到 Swift 的指针在同一个位置,但内容却不同;此外,最后两个打印语句产生不同的输出。

现在,如果我简单地注释掉行 A in demo,我会得到以下结果:

from swift: str = 'thisisastring'
from swift: strptr = 0x00000001003894b0
from swift: strptr.pointee = 0
from swift: strptr_cstring = 'S\212' 
from swift: String(cString: strptr) = ''
from c: got pointer 0x100469640 = thisisastring
from swift: ret_strptr = 0x0000000100469640 
from swift: ret_strptr.pointee = 48
from swift: ret_strptr_cstring = '48000010046964'
from swift: String(cString: ret_strptr) = '48000010046964'

这令人惊讶,因为调用 C 函数之前的所有内容都不正确。

如果我然后注释掉 B1 行并取消注释 B3(只是删除对 C 的调用),我会得到预期的输出:

from swift: str = 'thisisastring'
from swift: strptr = 0x00000001004809b0
from swift: strptr.pointee = 116
from swift: strptr_cstring = 'thisisastring'
from swift: String(cString: strptr) = 'thisisastring'
from swift: ret_strptr = 0x00000001004809b0 
from swift: ret_strptr.pointee = 116
from swift: ret_strptr_cstring = 'thisisastring'
from swift: String(cString: ret_strptr) = 'thisisastring'

如果我在未注释的 B2 行(注释掉 A、B1 和 B3)的情况下运行它,我会得到:

from swift: str = 'thisisastring'
from swift: strptr = 0x0000000100203550
from swift: strptr.pointee = 0
from swift: strptr_cstring = '2'
from swift: String(cString: strptr) = ''
from c: got pointer 0x100203550 = 
from swift: ret_strptr = 0x0000000100203550 
from swift: ret_strptr.pointee = 0
from swift: ret_strptr_cstring = ''
from swift: String(cString: ret_strptr) = ''

最后,如果我在未注释 A 和 B2 行(B1 和 B3 被注释掉)的情况下运行,我会得到预期的结果:

from swift: str = 'thisisastring'
from swift: strptr = 0x00000001005533d0
from swift: strptr.pointee = 116
from swift: strptr_cstring = 'thisisastring'
from swift: String(cString: strptr) = 'thisisastring'
from c: got pointer 0x1005533d0 = thisisastring
from swift: ret_strptr = 0x00000001005533d0 
from swift: ret_strptr.pointee = 116
from swift: ret_strptr_cstring = 'thisisastring'
from swift: String(cString: ret_strptr) = 'thisisastring'

在我看来,对 C 的调用以一种不可预测的方式占用了内存,但是我在这里做错了什么吗?

在 macOS Sierra、Xcode 8.0、Swift 3.0 上运行。

4

1 回答 1

0

如果你想要一个来自 NSString 的 C 字符串,使用cStringor getCString

于 2016-10-18T19:14:37.413 回答