Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++;和目标C 我有一个示例项目,在这里我为一个外部的C++库创建了一个C++包装类,我想在Objtovi.C._C++_Objective C_Ios - Fatal编程技术网

在C++;和目标C 我有一个示例项目,在这里我为一个外部的C++库创建了一个C++包装类,我想在Objtovi.C.

在C++;和目标C 我有一个示例项目,在这里我为一个外部的C++库创建了一个C++包装类,我想在Objtovi.C.,c++,objective-c,ios,C++,Objective C,Ios,我不明白为什么我返回的指针有时是正确的,有时是错误的。以下是示例输出: 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

我不明白为什么我返回的指针有时是正确的,有时是错误的。以下是示例输出:

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
在上面的输出中,您可以看到第一次和第二次单击按钮会输出我期望的结果。在其他每次单击中,返回值或转换值无效。我假设这是因为我的指针指向了一个我没有预料到的地址。在多次运行应用程序时,任何按钮的点击都可能是对的或错的

我也尝试了一个单一的线程,但经历了类似的结果

完整的代码已打开,但以下是重要的部分

ViewController.m

#import "ViewController.h"

extern const char * CompressCodeData(const char * strToCompress);

@implementation ViewController

...

// IBAction on the button
- (IBAction)testNow:(id)sender 
{
    [self performSelectorInBackground:@selector(analyze) withObject:nil];
}

- (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
SampleWrapper.cpp

#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;
    }

}
#包括
#包括
#包括
使用名称空间std;
外部“C”
{
外部无效NSLog(CFStringRef格式,…);
/**
*此函数仅包装库函数,以便
*它可用于objective-c。
*/
const char*CompressCodeData(const char*strotcompress)
{
常量字符串s(strToCompress);
//在C++库中调用静态方法
//简化这个测试用例。
//const char*result=SomeStaticLibraryFunction;
const char*result=s.c_str();
NSLog(CFSTR(“压缩%s”),结果);
返回结果;
}
}

您正在返回指向已解除分配的at对象的指针

const string s(strToCompress);
…
const char *result = s.c_str();

NSLog(CFSTR("In Compress %s"), result);
return result;
s
CompressCodeData()
函数结束后不存在,因此指向其内部内存的指针无效


您可以分配一块内存来保存响应,但释放响应将取决于调用方

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());
另一个解决方案是传入内存以将数据存储到中

char compressed[2048]; // 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[s.length() < size ? s.length() : size-1] = '\0';
char压缩[2048];//或者别的什么!
CompressCodeData(testData,压缩,sizeof(压缩));
NSLog(@“返回值=%s”,已压缩);
NSString*casted=[NSString stringWithCString:压缩
编码:NSASCIIStringEncoding];
NSLog(@“铸造值=%@\n\n”,铸造);
…
void CompressCodeData(常量字符*strotcompress,字符*结果,大小)
…
s、 副本(结果,大小-1);
结果[s.length()
您正在返回指向已解除分配的at对象的指针

const string s(strToCompress);
…
const char *result = s.c_str();

NSLog(CFSTR("In Compress %s"), result);
return result;
s
CompressCodeData()
函数结束后不存在,因此指向其内部内存的指针无效


您可以分配一块内存来保存响应,但释放响应将取决于调用方

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());
另一个解决方案是传入内存以将数据存储到中

char compressed[2048]; // 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[s.length() < size ? s.length() : size-1] = '\0';
char压缩[2048];//或者别的什么!
CompressCodeData(testData,压缩,sizeof(压缩));
NSLog(@“返回值=%s”,已压缩);
NSString*casted=[NSString stringWithCString:压缩
编码:NSASCIIStringEncoding];
NSLog(@“铸造值=%@\n\n”,铸造);
…
void CompressCodeData(常量字符*strotcompress,字符*结果,大小)
…
s、 副本(结果,大小-1);
结果[s.length()