c++ - 在 C++ 和 Objective-C 中使用字符串指针
<p><p>我有一个示例项目 <a href="https://github.com/jamesor/TestCase" rel="noreferrer noopener nofollow">here on github</a>我为要在 Objective-C 中使用的外部 C++ 库创建了一个 C++ 包装类。</p>
<p>我不明白为什么我返回的指针有时正确有时错误。这是示例输出:</p>
<pre><code>Test Data = 43343008
In Compress 43343008
Returned Value = 43343008
Casted Value = 43343008
Test Data = 2239023
In Compress 2239023
Returned Value = 2239023
Casted Value = 2239023
Test Data = 29459973
In Compress 29459973
Returned Value = 29459973
Casted Value = l.remote
Test Data = 64019670
In Compress 64019670
Returned Value =
Casted Value = stem.syslog.master
</code></pre>
<p>在上面的输出中,您可以看到按钮的第一次和第二次单击输出了我期望的结果。在其他每一次点击中,返回的值或转换的值都是无效的。我假设这是因为我的指针指向一个我没想到的地址。多次运行应用程序时,任何按钮单击都可能是对的或错误的。</p>
<p>我也尝试过使用单线程,但结果相似。</p>
<p>完整的代码在 <a href="https://github.com/jamesor/TestCase" rel="noreferrer noopener nofollow">github</a>但这里是重要的部分。</p>
<p><strong>ViewController.m</strong></p>
<pre><code>#import "ViewController.h"
extern const char * CompressCodeData(const char * strToCompress);
@implementation ViewController
...
// IBAction on the button
- (IBAction)testNow:(id)sender
{
;
}
- (void)analyze
{
@synchronized(self) {
const char *testData = [[NSString stringWithFormat:@"%d",
(int)(arc4random() % 100000000)] UTF8String];
NSLog(@"Test Data = %s", testData);
const char *compressed = CompressCodeData(testData);
NSLog(@"Returned Value = %s", compressed);
NSString *casted = [NSString stringWithCString:compressed
encoding:NSASCIIStringEncoding];
NSLog(@"Casted Value = %@\n\n", casted);
}
}
@end
</code></pre>
<p><strong>SampleWrapper.cpp</strong></p>
<pre><code>#include <iostream>
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
using namespace std;
extern "C"
{
extern void NSLog(CFStringRef format, ...);
/**
* This function simply wraps a library function so that
* it can be used in objective-c.
*/
const char * CompressCodeData(const char * strToCompress)
{
const string s(strToCompress);
// Omitted call to static method in c++ library
// to simplify this test case.
//const char *result = SomeStaticLibraryFunction(s);
const char *result = s.c_str();
NSLog(CFSTR("In Compress %s"), result);
return result;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您正在返回一个指向已释放对象的指针。</p>
<pre><code>const string s(strToCompress);
…
const char *result = s.c_str();
NSLog(CFSTR("In Compress %s"), result);
return result;
</code></pre>
<p><code>s</code>在<code>CompressCodeData()</code>函数结束后不存在,所以指向它的内存的指针是无效的。</p>
<hr/>
<p>您可以分配一 block 内存来保存响应,但要由调用者来释放它。</p>
<pre><code>char *compressed = CompressCodeData(testData);
NSLog(@"Returned Value = %s", compressed);
NSString *casted = [NSString stringWithCString:compressed
encoding:NSASCIIStringEncoding];
free(compressed);
NSLog(@"Casted Value = %@\n\n", casted);
…
const char * CompressCodeData(const char * strToCompress)
…
char *result = strdup(s.c_str());
</code></pre>
<p>另一种解决方案是传入内存来存储数据。</p>
<pre><code>char compressed; // Or whatever!
CompressCodeData(testData, compressed, sizeof(compressed));
NSLog(@"Returned Value = %s", compressed);
NSString *casted = [NSString stringWithCString:compressed
encoding:NSASCIIStringEncoding];
NSLog(@"Casted Value = %@\n\n", casted);
…
void CompressCodeData(const char * strToCompress, char *result, size_t size)
…
s.copy(result, size - 1);
result = '\0';
</code></pre></p>
<p style="font-size: 20px;">关于c++ - 在 C++ 和 Objective-C 中使用字符串指针,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/11019072/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/11019072/
</a>
</p>
页:
[1]