Re: checking array indices
From: Ricardo Gibert (nospamgibert_at_cox.net)
Date: 07/25/04
- Next message: Gordon Burditt: "Re: data types question"
- Previous message: Don_at_home.com: "Re: Osama Found Hanged"
- In reply to: Keith Thompson: "Re: checking array indices"
- Next in thread: Keith Thompson: "Re: checking array indices"
- Reply: Keith Thompson: "Re: checking array indices"
- Reply: Giorgos Keramidas: "Re: checking array indices"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 25 Jul 2004 00:59:19 -0700
"Keith Thompson" <kst-u@mib.org> wrote in message news:ln658cv9ki.fsf@nuthaus.mib.org...
> Barry Schwarz <schwarzb@deloz.net> writes:
> > On Sat, 24 Jul 2004 17:26:10 -0700, "Ricardo Gibert"
> > <nospamgibert@cox.net> wrote:
> > >"Sterten" <sterten@aol.com> wrote in message
> > >news:20040724025056.05916.00002222@mb-m05.aol.com...
> > >>
> > >> when I define
> > >> int R[99];
> > >> and then later access it with
> > >> x=R[r];C[x]=7;
> > >> ...
> > >> but x happens to be <0 or >99 , then the program's behavious
> > >> becomes unpredictable.
> > >
> > >You must mean x < 0 or x >= 99 in the above.
> > >
> > >You can try this:
> > >
> > > assert((unsigned) i < 99);
> >
> > Why the cast. If i contains a sufficiently large negative value, your
> > assert will be true but your next statement will invoke undefined
> > behavior.
> >
> > > t = R[i];
>
> No, there is no negative value of type int that yields a value less
> than 99 when converted to unsigned int. (There might be exotic
> representations where this isn't the case.)
The idea that, "There might be some exotic implementation where this isn't the case," hadn't occurred to me. I can't imagine what it
would be like. Hmmm.
In any case, on two's complement machines, a good optimizing compiler will convert "i >= 0 && i < 99" to "(unsigned) i < 99", since
this saves several assembly instructions. The cast from int to unsigned consumes zero machine cycles on two's complement machines.
In an assert, this is not important, but anywhere else, this is a nice trick. I just thought I throw it into my post to make people
think.
>
> But I'd still be more comfortable with
>
> assert(i >= 0 && i < 99);
For an assert() or any code that is serious about being readable for that matter, your way is much better.
>
> (assuming that assert is a good way to do the check in the first
> place).
>
> --
> Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.
- Next message: Gordon Burditt: "Re: data types question"
- Previous message: Don_at_home.com: "Re: Osama Found Hanged"
- In reply to: Keith Thompson: "Re: checking array indices"
- Next in thread: Keith Thompson: "Re: checking array indices"
- Reply: Keith Thompson: "Re: checking array indices"
- Reply: Giorgos Keramidas: "Re: checking array indices"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|