Sujet : Re: avoiding strdup()
De : spibou (at) *nospam* gmail.com (Spiros Bousbouras)
Groupes : comp.lang.cDate : 11. Mar 2024, 20:57:22
Autres entêtes
Organisation : To protect and to server
Message-ID : <YkhvH0ibt4fqZu1NS@bongo-ra.co>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
On Mon, 11 Mar 2024 19:35:11 +0200
Michael S <
already5chosen@yahoo.com> wrote:
Some people would say that writing code (a handler for allocation
returning NULL) that either can't be tested in principle or can be
tested only in principle, but most certainly not tested in
practice, isn't a sign of a good programmer.
It can be tested in practice by explicitly inserting failure code , perhaps
activated by a macro :
pointer = malloc(...) ;
if (pointer == 0 || testN) { ... handle error ... } ;
It clutters the code but it's testable.
Myself, I still tend to code this checks, but
(1) my main targets are not Linux with overcommit, so the
chance of allocation returning NULL could be estimated like "not going
to happen" rather than "can't happen".
(2) I am old full that like his unreasonable old habits
What's the practical difference between "not going to happen" and
"can't happen" ? Practically , you can never know that it can't happen
because overcommit is a matter of configuration of the Linux system.