Can glib be used in an unobtrusive way?

I searched for a good universal library for C on top of the standard C library and saw some suggestions on using glib . How "intrusive" is it in your code? To explain what I mean by compulsiveness, the first thing I noticed in the reference guide was a section of the main types , thinking to myself: "What, am I going to start using gint , gchar and gprefixing geverything gin gmy gcode gnow gnow?"

More generally, can you use it only locally if other functions or files in the code should not know about its use? Does this cause certain assumptions for your code or restrictions on the compilation / binding process? Does most runtime memory require global data structures? and etc.

+7
c coding-style glib
Jul 03 '13 at 12:20
source share
3 answers

The most annoying thing about glib is that any program or library that uses it is not resilient. It unconditionally calls abort when malloc crashes, and there is nothing you can do to fix it, because the entire library is designed around the concept that their internal distribution function g_malloc "cannot fail"

As for the ugly "g" types, you definitely don't need any throws. These types are 100% equivalent to standard types and basically just come off the glib's early (wrong) design. Unfortunately, glib developers lack an understanding of C, as evidenced by this FAQ:

Why use g_print, g_malloc, g_strdup and other glib functions?

"Regarding g_malloc (), g_free () and siblings, these functions are much safer than their libc equivalents. For example, g_free () just returns if called with NULL.

(Source: https://developer.gnome.org/gtk-faq/stable/x908.html )

FYI, free(NULL) acts fine C and does the same: it just returns.

+5
Jul 03 '13 at 12:39 on
source share

I have been using GLib professionally for over 6 years and have nothing but praise for it. It is very lightweight, with many useful utilities such as lists, hash tables, rand functions, io libraries, streams / mutexes / conventions, and even GObject. Everything is done in a portable way. In fact, we compiled the same GLib code for Windows, OSX, Linux, Solaris, iOS, Android and Arm-Linux without any icons on the GLib side.

In terms of obsession, I definitely “bought in g,” and there is no doubt that it was extremely useful for creating stable portable code at high speed. Perhaps specifically when it comes to writing advanced tests.

And if g_malloc is not suitable for your purpose, just use malloc instead, which, of course, is suitable for everything.

+2
Jul 04 '13 at 9:36 on
source share

Of course, you can "forget about it elsewhere", if, of course, these other places somehow do not interact with the glib code, then there is a connection (and maybe you really are not "somewhere else location ").

You do not need to use types that are just regular types with g added ( gchar , gint , etc.); they are guaranteed to be the same as char , int and so on. You never need to resort to / from gint , for example.

I think the intention is that the application code should never use gint , it just turns on so that the glib code can be more consistent.

+1
Jul 03 '13 at 12:24
source share



All Articles