glib是否可以以不显眼的方式使用? [英] Is glib usable in an unobstrusive way?

查看:179
本文介绍了glib是否可以以不显眼的方式使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在标准C库之上寻找一个好的通用程序库C ,若干建议要使用 glib 。你的代码中是如何突出的?为了解释我的意思是什么意思,我在参考手册中注意到的第一件事是< a href =https://developer.gnome.org/glib/2.36/glib-Basic-Types.html =nofollow>基本类型部分,思考自己,什么,我要去开始使用 gint gchar 和gprefixing geverything gin gmy gcode gnow?

I was looking for a good general-purpose library for C on top of the standard C library, and have seen several suggestions to use glib. How 'obtrusive' is it in your code? To explain what I mean by obtrusiveness, the first thing I noticed in the reference manual is the basic types section, thinking to myself, "what, am I going to start using gint, gchar, and gprefixing geverything gin gmy gcode gnow?"

更一般地说,你只能在本地使用它,没有其他函数或你的代码中的文件必须知道它的使用?它是否强制您的代码的某些假设,或编译/链接过程的约束?它在运行时为全局数据结构占用了大量的内存吗?等等。

More generally, can you use it only locally without other functions or files in your code having to be aware of its use? Does it force certain assumptions on your code, or constraints on your compilation/linking process? Does it take up a lot of memory in runtime for global data structures? etc.

推荐答案

glib最恐怖的事情是任何使用它的程序或库对资源耗尽是不稳定的。当 malloc 失败时,它无条件地调用 abort ,并且没有什么可以做的来解决这个问题,因为整个库是围绕其内部分配函数 g_malloc 不能失败的概念

The most obtrustive thing about glib is that any program or library using it is non-robust against resource exhaustion. It unconditionally calls abort when malloc fails and there's nothing you can do to fix this, as the entire library is designed around the concept that their internal allocation function g_malloc "can't fail"

至于丑陋的g类型,你绝对不需要任何cast。类型是100%等同于标准类型,并且基本上只是从glib的早期(mis)设计。不幸的是,glib开发人员缺乏对C的很多理解,这是常见问题证明:

As for the ugly "g" types, you definitely don't need any casts. The types are 100% equivalent to the standard types, and are basically just cruft from the early (mis)design of glib. Unfortunately the glib developers lack much understanding of C, as evidenced by this FAQ:


为什么使用g_print,g_malloc,g_strdup和glib函数?

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

关于g_malloc(),g_free()和siblings,这些函数比libc等效的函数更安全,例如,g_free

"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.

(资料来源: https://developer.gnome.org/gtk-faq/stable/x908.html

FYI, free(NULL)是完全有效的C,并做同样的事情:它只是返回。

FYI, free(NULL) is perfectly valid C, and does the exact same thing: it just returns.

这篇关于glib是否可以以不显眼的方式使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆