Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Ian Collins Newsgroups: comp.lang.c Subject: Re: Memory corruption on freeing a pointer to pointer Date: Tue, 27 Aug 2013 09:42:53 +1200 Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net H+1+1RcHiIqWrzqwf+VYIwEr9HSVwZ6gX/U88B4iCDB7aMy5v1 Cancel-Lock: sha1:FI6wsyDpHrgzgqnNOAr6FJ4alpw= User-Agent: Mozilla/5.0 (X11; SunOS i86pc; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Xref: csiph.com comp.lang.c:35888 Ike Naar wrote: > On 2013-08-26, Ian Collins wrote: >> Ike Naar wrote: >>> On 2013-08-26, Ian Collins wrote: >>>> James Kuyper wrote: >>>>> On 08/26/2013 02:40 AM, Ike Naar wrote: >>>>>> On 2013-08-25, Keith Thompson wrote: >>>>>>> James Kuyper writes: >>>>>>>> On 08/24/2013 03:05 PM, Sharwan Joram wrote: >>>>>>> [...] >>>>>>>>> if ( NULL == parameters[parametercount]){ >>>>>>> >>>>>>> This is what's known as a "Yoda conndition" >>>>>>> . I know that a lot of >>>>>>> programmers like them, and for somewhat valid reasons, but personally I >>>>>>> find them jarring and unnecessary. Personally, I'd write that as: >>>>>>> >>>>>>> if (parameters[parametercount] == NULL) { >>>>>> >>>>>> Does it matter? The == operator is symmetric, (X==Y) == (Y==X). >>>>> >>>>> Yes, it matters - because it's intended to protect against the typo >>>>> which replaces == with =, by ensuring that it would cause a constraint >>>>> violation. The assignment operator is very definitely not symmetric. >>>> >>>> The best way to protect against that particular typo is decent unit tests. >>> >>> Do not overestimate the value of unit tests. >>> Let's take, for example, a piece of code that multiplies two >>> 32-bit numbers to obtain a 64-bit result. >> >> What has that got to do with this particular typo? > > Everything. If you know the typo in your progam, you > don't need tests to find it, you just fix the typo. > If you don't know there's a typo, the only way to be sure you > find one is to test all possible cases. But you will know as soon as you add the code and it fails its test. -- Ian Collins