- 问题:
char *buf = "EC|RN0851|PW0851|LC8,0|";
char *h = "hhh";
strcat(h, buf);
strcpy(h, buf);
在ARC机制下使用ARC管理机制外的函数操作内存,程序直接崩溃,物错误提示
- 解决方案:
char *c = (char *) malloc(strlen(h) + strlen(buf));
PS:
//ARC机制下操作内存需要申请
char *str = "EC|RN0851|PW0851|LC8,0|";
char pried = 0x02;
char end = 0x03;
char check = 0x01;
char *buff = (char *) malloc(3 + strlen(str));
strncpy(buff, &pried,1);
strncat(buff, str, strlen(str));
strncat(buff, &end,1);
strncat(buff, &check,1);
NSLog(@"length:%ld",strlen(&pried));//输出为4,所以单字符时可以写成数组等,或者如上
for (int i = 0; i < strlen(buff); i++) {
printf("-%x", buff[i]);
}
网友评论