[gameprogrammer] Re: VC++ array woes.

On Wed, 25 Aug 2004 08:23:10 -0600, "Jason Clark" <jclark@xxxxxxxx>
said:
> Thanks to everyone who replied.
> I think I have a more heinous problem. I have a hash define that is
> getting printed with different values at different points in my program,
> so something is definitely wrong!

Guess what...  This is why hash defines are bad.  If it's in your code,
switch it to a named constant variable, eg:

#define MAX_WIDTH 300

becomes

static const int MAX_WIDTH = 300;

(May or may not be static depending if it is in a header file or source
file.)

Then you'll get a compiler error instead of random strange behaviour.

> As far as the (NULL =3D=3D ....), that is a trick I learned from an old
> timer and it's stuck with me. It's great for newbies and people, like
> me, that race through there code.  By putting the value compared against
> on the left, if I mistype my comparative '=3D=3D' as an assignment =
> operator
> '=3D', the compiler will give me an lvalue warning and save me from
> shooting myself in the foot.
> i.e.,
> if (3 =3D=3D myVar) ... vs if (myVar =3D 3)
> 
> A cool little trick I think.

Yeah, putting the constant on the left side of the equality test can
save a lot of trouble.  But I don't think that was what we were talking
about.  I was talking about doing the actual assignment in the test of
the if statement, eg:

if (NULL == (tmp = (int*)malloc(sizeof(int) * size)))
{ handle error }

should be:

tmp = (int*)malloc(sizeof(int) * size);
if (NULL == tmp)
{ handle error }

It's cleaner and less error prone, and it gets rid of one level of
parentheses (which is a maintenance problem).

Dave.
-- 
  Dave Slutzkin
  Melbourne, Australia
  daveslutzkin@xxxxxxxxxxx



---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: