连接两个字符串时g_strjoin崩溃

时间:2014-09-09 12:09:35

标签: c glib

我正在使用glib-2.0加入两个简单的字符串。这是我的代码:

int main() {

    gchar *a = "12";
    gchar *b = "1231212";

    printf("a: %s\n", a);
    printf("b: %s\n", b);
    gchar *c = g_strjoin(",", a, b);
    printf("c: %s\n", c);

    return EXIT_SUCCESS;
}

它在g_strjoin(",", a, b)崩溃了。核心文件没有显示太多信息:

Using host libthread_db library "/lib64/libthread_db.so.1".
Core was generated by `./server'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  strlen () at ../sysdeps/x86_64/strlen.S:137
137     FIND_ZERO

我做错了吗?我在fedora 20上使用glib2-2.38.2-2.fc20.x86_64

1 个答案:

答案 0 :(得分:2)

由于g_strjoin()可以加入任意数量的字符串,因此必须使用NULL终止列表:

 gchar *c = g_strjoin(",", a, b, NULL );