- 今天遇到個問題,使用CJSON把一個cjson對象給一個cjson對象的時候報錯,是segment問題 ,原因是我在個cjson對象數據的時候,有幾個是char的,但是是個時間的字符串,一般20位就夠了,但是由于是通過modbus獲取的數據,初始化是內存存的是ff,這就造成了start_time[20],超了,但是居然沒有報錯,然后把這個start_time給了cjson也沒有報錯,但是把cJson給另外一個cjson的時候,就報錯了,查看cjson代碼,在cjson_free(item->string)的時候報的錯誤,因此修改了start_time[50] = {0}多給點吧,怕錯誤,就不報錯了
void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item)
{
if (!item)
return;
if (item->string)
cJSON_free(item->string);
item->string = cJSON_strdup(string);
cJSON_AddItemToArray(object, item);
}
sprintf(start_time,"20%02d-%02d-%02d %02d:%02d:%02d",jc_fixed.start_time_year,jc_fixed.start_time_mon,jc_fixed.start_time_dd,jc_fixed.start_time_hour,jc_fixed.start_time_min,jc_fixed.start_time_second);
????????sprintf(end_time,"20%02d-%02d-%02d %02d:%02d:%02d",jc_fixed.end_time_year,jc_fixed.end_time_mon,jc_fixed.end_time_dd,jc_fixed.end_time_hour,jc_fixed.end_time_min,jc_fixed.end_time_second);