GtkTreeIter指针失败

时间:2018-07-02 13:36:01

标签: gtk

对于GTK教程中有关设置树的以下代码:

 /* Create a model.  We are using the store model for now, though we
 * could use any other GtkTreeModel */

 store = gtk_tree_store_new (N_COLUMNS,
                                G_TYPE_STRING,
                                G_TYPE_STRING,
                                G_TYPE_STRING);


GtkTreeIter iter;

gtk_tree_store_append (store, &iter, NULL);

/* Acquire an iterator */

gtk_tree_store_set (store, &iter,
                    WORD, "antidisestablishmentarianism",
                    COUNT, "29",
                    LINES, "1,33,89",
                    -1);


gtk_tree_store_append (store, &iter, NULL);

gtk_tree_store_set (store, &iter,
                    WORD, "monkey",
                    COUNT, "2",
                    LINES, "1,55",
                    -1);

如果我不是声明对iter的引用,而是声明一个指针并将其传递,则代码将失败:

GtkTreeIter *iter;
gtk_tree_store_append (store, iter, NULL);

知道为什么吗?不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

GtkTreeIter *iter;
gtk_tree_store_append (store, iter, NULL);

在这里iter有什么价值?没有定义。 GtkTreeIter结构未分配到任何地方。传递此类指针的是UB。

您应该在堆栈(GtkTreeIter iter)或堆(GtkTreeIter * iter = g_new (GtkTreeIter, 1))上分配它