- 問題:
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]);
}