【发布时间】:2021-12-05 20:48:52
【问题描述】:
我有一个无限的 while 循环,我不确定我应该使用 char 数组还是 char 指针。该值不断被覆盖并用于其他功能。使用 char 指针,我知道可能存在内存泄漏,所以最好使用数组吗?
char *recv_data = NULL;
int main(){
.....
while(1){
.....
recv_data = cJSON_PrintUnformatted(root);
.....
}
}
或
char recv[256] = {0};
int main(){
.....
while(1){
.....
strcpy(recv, cJSON_PrintUnformatted(root));
.....
}
}
【问题讨论】:
-
第二个版本有内存泄漏,因为你永远无法释放函数返回的数组。
-
你是实现
cJSON_PrintUnformatted()功能的人吗?或者它是给定的,你必须使用它? -
@einpoklum
cJSON_PrintUnformatted()是一个标准的 JSON/cJSON 函数,用于打印 cJSON 对象/项目,它返回一个指向 char 的指针 -
C 没有“标准”JSON 库...你确定你使用的是好的吗?我不是他们的专家,但你考虑过替代品吗? This one 很受欢迎。