我正在关注iOS5上的rapidxml教程,不幸的是我的c有点生疏。下面的代码适用于较小的字符大小,但当我拉下较大的大小时,我会使用
EXC_BAD_ACCESS崩溃(我正在从RESTful接口中提取XML)。
# pragma mark - parser methods
-(void)parseIncomingFeed:(NSData *)xmlData:(NSString*)tag {
NSString *xmlStringOrg = [[NSString alloc] initWithData:xmlData encoding:NSASCIIStringEncoding];
NSLog(@"[xmlStringOrg length] = %-10d [xmlData length] = %d", [xmlStringOrg length],[xmlData length]);
char xmlString[[xmlStringOrg length]];
strcpy(xmlString, [xmlStringOrg UTF8String]); // <--- crashes here
...
以下是结果:
[xmlStringOrg length] = 13580 [xmlData length] = 13580
[xmlStringOrg length] = 115476 [xmlData length] = 115476
[xmlStringOrg length] = 154589 [xmlData length] = 154589
[xmlStringOrg length] = 1902 [xmlData length] = 1902
[xmlStringOrg length] = 23402 [xmlData length] = 23402
[xmlStringOrg length] = 93037 [xmlData length] = 93037
[xmlStringOrg length] = 1723 [xmlData length] = 1723
[xmlStringOrg length] = 172329 [xmlData length] = 172329
[xmlStringOrg length] = 3470671 [xmlData length] = 3470671
(boom! with EXE_BAD_ACCESS leading to: 0x3234af6e: push.w {r3, r4})
盲目地,我尝试了以下但没有运气:
NSString *xmlStringOrg = [[NSString alloc] initWithData:xmlData encoding:NSASCIIStringEncoding];
char xmlString[3600000];
strcpy(xmlString, [xmlStringOrg UTF8String]);
和
NSString *xmlStringOrg = [[NSString alloc] initWithData:xmlData encoding:NSASCIIStringEncoding];
char xmlString[[xmlStringOrg length]];
strncpy(xmlString, [xmlStringOrg UTF8String], [xmlStringOrg length]);
有什么想法吗?从很久以前的c类开始,感觉就像我需要大小超过X的块大小并单独处理它们但是我不知道从哪里开始.R
# pragma mark - parser methods
-(void)parseIncomingFeed:(NSData *)xmlData:(NSString*)tag {
NSString *xmlStringOrg = [[NSString alloc] initWithData:xmlData encoding:NSASCIIStringEncoding];
NSLog(@"[xmlStringOrg length] = %-10d [xmlData length] = %d", [xmlStringOrg length],[xmlData length]);
char *xmlString = (char*) malloc([xmlStringOrg length]);
strcpy(xmlString, [xmlStringOrg UTF8String]); // no more tears
...
free(xmlString);
}