From: kagel AT quasar DOT bloomberg DOT com Date: Mon, 15 Jan 1996 08:18:20 -0500 To: korpela AT islay DOT ssl DOT berkeley DOT edu Cc: djgpp AT delorie DOT com Subject: Re: (none) Reply-To: kagel AT quasar DOT bloomberg DOT com From: korpela AT islay DOT ssl DOT berkeley DOT edu (Eric J. Korpela) Date: 11 Jan 1996 18:23:27 GMT Content-Type: text Content-Length: 586 In article , wrote: > > if (!my_pointer) > my_pointer = (my_struct_t *)malloc( sizeof (my_struct_t) ); > >Or BETTER CODE but equivalent: > > if (my_pointer == (my_struct_t *)NULL) Is this really better? Could option 1 fail? I thought the C standard guaranteed that the top option would work. Eric -- Eric Korpela | An object at rest can never be korpela AT ssl DOT berkeley DOT edu | stopped. Click here for more info. There is no syntactic or semantic difference between these versions. However, I always prefer my code to be as self documenting as possible. Someone else looking at the first snippet, or a similar test not accompanied by a malloc(), might not immediately realize the purpose of the test. The second immediately documents the test as a test for an uninitialized pointer of type (my_struct_t *). I have been writing code for 15 years and had the fortune, or mis-fortune, to have been coding for four years before I had to maintain anyone else's code. When I finally did I was appalled at the lack of documentation in the code and at how hard it was to understand the purpose of any block of code smaller than several pages. Looking back at my own code I got VERY ILL, mine was worse yet. It was an abject lesson which I mever forgot. While I, like everyone else, rarely have the time to add comments until the second or third pass at maintenance, I always make certain that my code is as self documented as possible. -- Art S. Kagel, kagel AT quasar DOT bloomberg DOT com A proverb is no proverb to you 'till life has illustrated it. -- John Keats