【发布时间】:2013-10-14 16:05:23
【问题描述】:
首先,我知道这应该在 SO 的某个地方得到回答,但我似乎找不到正确的帖子。因此,如果它是重复的,请指向我回答这个问题的帖子,我将删除它。
我有一个复制字符串的函数:
static int read_ad_content(json_t * root, char* content)
{
[.. stuff happens]
const char* tmp = json_string_value(json_content);
unsigned int size = strlen(tmp);
content = (char*)malloc(size + 1);
memcpy(content, tmp, size);
content[size] = '\0'; // <= I checked and content is NOT null here!
return 0;
}
我在我的主函数中这样称呼它:
char *ad_content;
if (read_ad_content(ad_json, ad_content) != 0)
{
log_err(argv, "Failed to extract information");
}
if (ad_content == NULL)
{
// <= I always end up here
}
我知道这应该很容易,但我就是不知道如何解决。
【问题讨论】: