Why use GLib functions?

后端 未结 7 532
野的像风
野的像风 2021-01-31 17:30

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

7条回答
  •  梦毁少年i
    2021-01-31 17:41

    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.

提交回复
热议问题