Search code examples
cgtkglib

Why use GLib functions?


While programming in C and GTK+, why is it "better" to use g_strdup_printf, g_free, g_strcmp0 etc... and fellow GLib functions?


Solution

  • In general, GLib's purpose is a utility and portability library. Those in itself are reasons to consider using it.

    The specific functions you mention all offer something extra on top of their C standard library variants:

    • g_strdup_printf is like sprintf, but actually allocates the buffer for you and saves you the guesswork of how large the buffer should be. (The return value should be g_free'd.)
    • g_free is like free, but checks for a NULL-pointer.
    • g_strcmp0 is like strcmp, but treats a NULL-pointer like an empty string, and thus sorts it in front.