Mail Archives: djgpp/1996/01/15/08:33:08
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 <DKzMM1 DOT KrA AT jade DOT mv DOT net>, <kagel AT quasar DOT bloomberg DOT com> 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.
<a href="http://www.cs.indiana.edu/finger/mofo.ssl.berkeley.edu/korpela/w">
Click here for more info.</a>
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
- Raw text -