glib是否可以不干扰使用? [英] Is glib usable in an unobtrusive way?

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

问题描述

我一直在标准C库的上方寻找良好的C通用库,使用 glib 的一些建议。您的代码中的干扰性如何?为了解释我的意思,我在参考手册中注意到的第一件事是基本类型部分,对自己说,什么,我是我将开始使用 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"

的概念类型,您绝对不需要任何强制类型转换。这些类型与标准类型100%等效,并且基本上只是从glib的早期(错误)设计中摘下来的。不幸的是,这个FAQ证明了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()和兄弟姐妹,这些函数比它们的libc等效项安全得多。例如,如果使用NULL调用,则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天全站免登陆