From 1b06b35d82a8b15d66710b5a39864e773fb23c4e Mon Sep 17 00:00:00 2001 From: Roland Illig Date: Wed, 22 Sep 2004 11:02:20 +0000 Subject: [PATCH] * HACKING: added advice for using g_strdup for a modifiable copy of a string. --- ChangeLog | 5 +++++ HACKING | 22 ++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 877ef1f42..4024b5bee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2004-09-22 Roland Illig + + * HACKING: added advice for using g_strdup for a modifiable + copy of a string. + 2004-09-19 Roland Illig * HACKING: added explanation for const_cast. diff --git a/HACKING b/HACKING index 3bc082d2d..c9f88ac8f 100644 --- a/HACKING +++ b/HACKING @@ -296,6 +296,22 @@ const_cast: We use many libraries that do not know about "const char *" and function does with the string, we should use g_strdup to pass dynamically allocated strings. +g_free: g_free handles NULL argument too, no need for the comparison. + Bad way: + if (old_dir) g_free (old_dir); + Right way: + g_free (old_dir); + +g_strdup: If you use g_strdup to create a local copy of a string, use + the following pattern to keep the reference. + + char * const pathref = g_strdup(argument); + /* ... */ + g_free (pathref); + + The "const" will make the pointer unmodifiable (local_copy++ + is not possible), but you can still modify the string contents. + g_strlcpy: Whenever you use this function, be sure to add "glibcompat.h" to the included headers. This is because in glib-1.2 there is no such function. @@ -306,9 +322,3 @@ size_t: This data type is suitable for expressing sizes of memory or the strncpy: Don't use this function in newly created code. It is slow, insecure and hard to use. A much better alternative is g_strlcpy (see there). - -g_free: g_free handles NULL argument too, no need for the comparison. - Bad way: - if (old_dir) g_free (old_dir); - Right way: - g_free (old_dir);