From: alainm AT news DOT RISQ DOT QC DOT ca (Alain Magloire) Subject: Re: Problems with MALLOC and FREE Newsgroups: comp.os.msdos.djgpp References: <82rh3j$sfu AT cantine DOT wu-wien DOT ac DOT at> <385EC088 DOT 8403BF22 AT ibm DOT net> <7GM74.500$PK3 DOT 5161 AT dfiatx1-snr1 DOT gtei DOT net> <385FE52F DOT E2F5572B AT east DOT sun DOT com> <38609B74 DOT 75D7B734 AT acm DOT org> X-Newsreader: TIN [version 1.2 PL2] Lines: 61 Message-ID: Date: Wed, 22 Dec 1999 17:37:58 GMT NNTP-Posting-Host: 132.206.63.174 X-Complaints-To: abuse AT mcgill DOT ca X-Trace: carnaval.risq.qc.ca 945884278 132.206.63.174 (Wed, 22 Dec 1999 12:37:58 EST) NNTP-Posting-Date: Wed, 22 Dec 1999 12:37:58 EST To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Very nicely said, Eric. It is worth a repost, see below the hole discussion ;-) I don't know why, specially newbies, they all blame the library or the compiler, when the programs crash. And 99.99%, it's in the user code. How many times, we've heard : "it works when I do this, but crash when I do that", or the true classic ..... "It used to work fine!!!!!" Well guess what, this what "undefined behaviour" is all about ;-) Was not a tool call YAMD, to help somewhere ? -- alain Eric Sosman (esosman AT acm DOT org) wrote: : Damian Yerrick wrote: : > : > "Eric Sosman" wrote in message : > news:385FE52F DOT E2F5572B AT east DOT sun DOT com... : > > Damian Yerrick wrote: : > > > [...] : > > > Oh, by the way, does anyone know what the standard : > > > says should happen when you free(NULL)? : > > : > > "If _ptr_ is a null pointer, no action occurs." : > : > Simplifies things a bit. : > : > #define Free(ptr) free(ptr); ptr = NULL : This is a poor idea on two counts. First, the macro : expansion is faulty and will fail badly in situations like : if (throw_it_away) : Free(ptr); : or (harder to fix) : Type **ptr; : ... : Free (*ptr++); : The second drawback is that NULLing a free'd pointer may : give an illusion of security which is completely false. If it : is to be effective, `ptr' must be the one and only copy of the : free'd pointer value anywhere in the program; setting `ptr' to : NULL will not affect any other now-invalid copies which may : reside elsewhere. Note, for example, that if `ptr' is a : function argument, the proposed Free() macro (in a corrected : version) would only change the function's local copy, not the : original in the function's caller. : It is much better -- in fact, it is essential -- to use : proper discipline in managing dynamic memory. Write your : programs so that the responsibility for acquiring and releasing : memory (and other resources, for that matter) is well controlled, : and don't count on unreliable tricks to cover up carelessness. : -- : Eric Sosman : esosman AT acm DOT org