From: Clayton Weaver
Subject: cowboy C
Date: 
Message-ID: <Pine.SUN.3.96.980702033500.11262A-100000@eskimo.com>
Re: NULL. Good to know. gcc has it ((void *)0) for C but C++
needs ((void) 0), according to some thread or other in comp.lang.c++.

There was actually a worse problem with the other example:

#define UPSHIFT8(v)  ((v) << 8)

/* assign v to 2nd byte of a */

#define SET_2nd(a, v)  (a &= 0xffff00ff; a |= UPSHIFT8(v)

This evaluates "a" twice (typical beginner's C error). That doesn't matter
if you pass SET_2nd an actual 32-bit int variable for a, but if you pass
it an expression using pointer arithmetic that evaluates to a 32-bit int,
it will do the pointer arithmetic twice and you'll get code which compiles
ok but gives wrong results.

int val = SET_2nd(*p++, some_8bitint);  /* lose */

So that really is "cowboy coding", it works only if the cpu is little
endian *and* you remember when using it not to use pointer arithmetic in
an expression for the "a' arg to the macro, *and* some maintainer years
down the road actually looks it up and sees that it is a a macro that
evaluates "a" twice instead of just assuming that it is safe to use just
like a function when reworking the code a little. 

The robust way to do it is to use an inline function, so that "*p++" type
arguments only get evaluated once, when computing the value to pass as the
function argument, even if their value is used more than once.

"So 3 months later the homeowner with the ocean view buys a caravan and
parks it behind the house, changing the windstream, and 3 years later
the paint falls off of the back of the house."

The point about "cowboy coding" (was this a Texas A&M joke?) is thus that
it is only reliable in ideal circumstances. It's not designed for 
fault-tolerance and indefinite deployment. The usual response to criticism
of that style is probably "so what? This only took half a day to code,
if it breaks I'll fix it." 

Not all C programmers, not even most maybe, are really "cowboy coders",
though. A loose end for many is like an itch that itches every time you
see it or think about it until you fix it.

Regards, Clayton Weaver  ······@eskimo.com  (Seattle)

-- 

Regards, Clayton Weaver  ······@eskimo.com  (Seattle)

"Since this is a technical newsgroup and has mostly done a pretty good job
From: Martin Cracauer
Subject: Re: cowboy C
Date: 
Message-ID: <wmk95w126u.fsf@wavehh.hanse.de>
Clayton Weaver <······@eskimo.com> writes:

> Re: NULL. Good to know. gcc has it ((void *)0) for C but C++
> needs ((void) 0), according to some thread or other in comp.lang.c++.

Hm, the (non-C++) gcc I use has #define NULL 0. Read the C FAQ why
this is right.

Ob newsgroup relevance: There are sloppy coders in all languages, if
anything, both C *and* Lisp have less. Both are languages of
free-will-choice for reasonable people for serious tasks they have a
personal interest in and that is what raises code quality.

Martin