Skip to content

Commit

Permalink
* HACKING: added advice for using g_strdup for a modifiable
Browse files Browse the repository at this point in the history
	copy of a string.
  • Loading branch information
rillig committed Sep 22, 2004
1 parent c524515 commit 1b06b35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2004-09-22 Roland Illig <[email protected]>

* HACKING: added advice for using g_strdup for a modifiable
copy of a string.

2004-09-19 Roland Illig <[email protected]>

* HACKING: added explanation for const_cast.
Expand Down
22 changes: 16 additions & 6 deletions HACKING
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

0 comments on commit 1b06b35

Please sign in to comment.