g_list_copy_deep and g_slist_copy_deep in GLib

Share on Facebook
Share on LinkedIn

If you have in your code things like that:

copy = g_slist_copy (list);
g_slist_foreach (copy, g_object_ref, NULL);

In GLib 2.34 (to be released soon) you can do:

copy = g_slist_copy_deep (list, g_object_ref, NULL);

Thus eliminating another loop in the list (one loop is done when copying the list).

In other words, there’s now a function to do a deep (full) copy of a GList and GSList. It accepts, besides the list to be cloned, a function as a second argument, and a user data as the third argument. That function will be called for each element in the list.

To free the new list, use g_list_free_full().

3 comments ↓

#1 Matthew Barnes on 06.21.12 at 19:12

Please don’t forget GQueue.

#2 Robert Ancell on 06.21.12 at 22:22

That’s awsome!

#3 Benjamim Janeiro on 06.27.12 at 9:43

On the Ubuntu Vinagre remote control program please consider to add a space (a line) to add comments.
Example : 192.168.0.10 Comment: Computador do João
Awsome job !!
Obrigado.