diff options
author | meyering <meyering@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-04-20 18:18:43 +0000 |
---|---|---|
committer | meyering <meyering@138bc75d-0d04-0410-961f-82ee72b054a4> | 2011-04-20 18:18:43 +0000 |
commit | ff3aed6233b16f02b26f82b3dac7d604aac42641 (patch) | |
tree | 383467c3fd67db83bd7cf20b900410bf5d8b1a80 /gcc/README.Portability | |
parent | 39efead85603b7fd9de8f56c376fafe6deb1f6b2 (diff) | |
download | gcc-ff3aed6233b16f02b26f82b3dac7d604aac42641.tar.gz |
discourage unnecessary use of if before free
* README.Portability: Explain why "if (P) free (P)" is best avoided.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@172784 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/README.Portability')
-rw-r--r-- | gcc/README.Portability | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/gcc/README.Portability b/gcc/README.Portability index 32a33e27bec..4101a2f7b42 100644 --- a/gcc/README.Portability +++ b/gcc/README.Portability @@ -51,14 +51,28 @@ foo (bar, ) needs to be coded in some other way. -free and realloc ----------------- +Avoid unnecessary test before free +---------------------------------- -Some implementations crash upon attempts to free or realloc the null -pointer. Thus if mem might be null, you need to write +Since SunOS 4 stopped being a reasonable portability target, +(which happened around 2007) there has been no need to guard +against "free (NULL)". Thus, any guard like the following +constitutes a redundant test: + + if (P) + free (P); + +It is better to avoid the test.[*] +Instead, simply free P, regardless of whether it is NULL. + +[*] However, if your profiling exposes a test like this in a +performance-critical loop, say where P is nearly always NULL, and +the cost of calling free on a NULL pointer would be prohibitively +high, consider using __builtin_expect, e.g., like this: + + if (__builtin_expect (ptr != NULL, 0)) + free (ptr); - if (mem) - free (mem); Trigraphs @@ -194,4 +208,3 @@ o Passing incorrect types to fprintf and friends. o Adding a function declaration for a module declared in another file to a .c file instead of to a .h file. - |