[Geany-devel] [Patch] after g_strndup, use g_free instead of free

Enrico Tröger enrico.troeger at xxxxx
Wed May 19 21:10:13 UTC 2010


On Sun, 16 May 2010 22:40:25 +0200, Colomban wrote:

> Le dimanche 16 mai 2010 à 19:46 +0200, Enrico Tröger a écrit :
> > Just for clarity, there is no technical requirement that
> > g_strndup() or similar should be followed by a g_free() instead of
> > free(). g_free() simply calls free() with the difference that it
> > checks for a NULL pointer before which is good in most cases.
> > And using g_free() there is also better for readability.
> Sorry but that's not exactly true: it is in the context of Geany, but

No need for sorry, if at all, I had to say sorry :).


> it isn't in general. g_free() frees a memory chunk with the
> registered free function in the GLib's memory VTable, which by
> default is libc's free() -- and then g_free() and free() are
> equivalent. But if for a reason or another the VTable is set to
> another one, a plain free() may be invalid since the memory may not
> have been allocated by libc's malloc(), calloc() or realloc().

Exactly.
Sorry for not being exact enough, actually I didn't mention the above
facts on purpose to not complicate things more than necessary.


> And free(NULL) is always a valid call ;)

Oh, oops. I just checked free(3) on my system and it actually says that
if NULL is passed, no operation is performed. I was quite sure that
results in undefined behaviour. No idea how I got this idea...maybe
it's not true for all systems (systems other than Linux, maybe some
Unices or Windows or who knows :D).

Regards,
Enrico

-- 
Get my GPG key from http://www.uvena.de/pub.asc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.geany.org/pipermail/devel/attachments/20100519/67530014/attachment.pgp>


More information about the Devel mailing list